Extract objects using Reflection
-
11-06-2021 - |
문제
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
p1
andc1
both so i can check its typethe 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)
해결책
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.
제휴하지 않습니다 StackOverflow