Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this by using reflection to walk the object hierarchy and look for fields that reference the object you're looking for. Here's a function I use to accomplish this. You must provide a root object for its search.</p> <p>This could probably be improved upon if you can look at the root stack frame and walk the local variables in there, but for my purposes, the app usually knows the rootmost object that it cares about.</p> <pre><code>// Spew all references to obj throughout the object hierarchy. public static void FindReferences( object appRootObject, object obj ) { Stack&lt;ReferencePath&gt; stack = new Stack&lt;ReferencePath&gt;(); FindReferences_R( stack, appRootObject, obj ); } struct ReferencePath { public ReferencePath( object parentObj, FieldInfo parentField ) { m_ParentObject = parentObj; m_ParentField = parentField; } public object m_ParentObject; public FieldInfo m_ParentField; } static void PrintReferencePath( Stack&lt; ReferencePath &gt; stack ) { string s = "RootObject"; foreach ( ReferencePath path in stack.ToArray().Reverse() ) { s += "." + path.m_ParentField.Name; } System.Console.WriteLine( s ); } static bool StackContainsParent( Stack&lt; ReferencePath &gt; stack, object obj ) { foreach ( ReferencePath path in stack ) { if ( path.m_ParentObject == obj ) return true; } return false; } static void FindReferences_R( Stack&lt; ReferencePath &gt; stack, object curParent, object obj ) { Type parentType = curParent.GetType(); foreach ( MemberInfo memberInfo in parentType.GetMembers( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ) ) { FieldInfo fieldInfo = memberInfo as FieldInfo; if ( fieldInfo == null ) continue; Type fieldType = fieldInfo.FieldType; if ( !fieldType.IsClass ) continue; object value = fieldInfo.GetValue( curParent ); if ( value == null ) continue; stack.Push( new ReferencePath( curParent, fieldInfo ) ); if ( value == obj ) { PrintReferencePath( stack ); } // Recurse, but don't recurse forever. if ( !StackContainsParent( stack, value ) ) { FindReferences_R( stack, value, obj ); } stack.Pop(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload