English 中文(简体)
使用反射提取对象
原标题:Extract objects using Reflection

在使用反射时,可以提取对象或变量,无论 stistic 与否。 例如 。

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

有没有可能提取

  1. p1 c1 两者都是为了让我检查其类型

  2. p1. clone () 的方法调用

到目前为止,这就是我对一个含有 0 元素的阵列所做的。

我从其他文件上加载大会

var name = System.Reflection.Assembly.LoadFile(open.FileName);
name.GetType("PrototypePattern.MainApp").GetFields(BindingFlags.GetField|BindingFlags.Instance)
最佳回答

如果您想要提取所有字段( 私人/ 公共/ 静态/ 非静态) 使用 :

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

< 强力 > 编辑 < /强 >

您无法获得变量和方法使用简单的反射来调用 < em> 侧面 < / em > 方法。 这需要实际读取方法的字节代码并分析它 — — 并非易事 。

如果你仍然热衷于它,我建议查看图书馆,例如mono.Cecil CCI

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签