Domanda

While using reflection is it possible to extract objects or variables no matter static or not. for example

class MainApp
{
    static void Main()
    {            
        ConcretePrototype1 p1 = new ConcretePrototype1("I");
        ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
        Console.WriteLine("Cloned: {0}", c1.Id);
    }
}

Is it possible to extract

  1. p1 and c1 both so i can check its type

  2. the method call from p1.clone()

so far this is what i have done with a resulting array with 0 elements

I am loading Assembly from other file

var name = System.Reflection.Assembly.LoadFile(open.FileName);
name.GetType("PrototypePattern.MainApp").GetFields(BindingFlags.GetField|BindingFlags.Instance)
È stato utile?

Soluzione

If you want to extract all fields (private/public/static/non-static) use:

type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)

Edit

You cannot get variables and methods calls inside methods using simple reflection. This requires actually reading the bytecodes of the method and analyzing it - not an easy task.

If you're still keen on it, I suggest checking out libraries such as Mono.Cecil or CCI.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top