使用反射实例化类并调用方法
下面是我从类对象获取属性值的方法。如何通过将类名作为 String 传递来实例化类来实现相同的目的。实例化后,我想使用同一页面上提供的方法“LoadFromXmlData()”。这可以做到吗?
private string GetPropertyValue(string propName, string className)
{
Class1 _Class1;
Class2 _Class2;
Class3 _Class3;
if (className.Equals("Class1"))
{
_Class1 = new Class1();
_Class1 = (Class1)Class1.LoadFromXmlData(typeof(Class1), myData.ToString());
return _Class1.GetType().GetProperty(propName).GetValue(_Class1, null).ToString();
}
else if (ActionClassName.Equals("TicketActionDef"))
{
_Class2 = new Class2();
_Class2 = (Class2)Class2.LoadFromXmlData(typeof(Class2), myData.ToString());
return __Class2.GetType().GetProperty(propName).GetValue(_Class2, null).ToString();
}
else
{
_Class3 = new Class3();
_Class3 = (Class1)Class1.LoadFromXmlData(typeof(Class3), myData.ToString());
return __Class3.GetType().GetProperty(propName).GetValue(_Class3, null).ToString();
}
}
我将按如下方式加载我的 DLL。这之后我应该如何进行。请帮忙,我对反思很陌生。
Assembly assembly = Assembly.GetAssembly(typeof(MyAdapter));
MyAdapter currentEventObject = (MyAdapter)assembly.CreateInstance(className);
谢谢
Below is my method to get a property value from a class object. How can I achieve the same by instantiating the class by passing the class name as String. After instantiating, I want to use the method 'LoadFromXmlData()' which is available on the same page. Can this be done?
private string GetPropertyValue(string propName, string className)
{
Class1 _Class1;
Class2 _Class2;
Class3 _Class3;
if (className.Equals("Class1"))
{
_Class1 = new Class1();
_Class1 = (Class1)Class1.LoadFromXmlData(typeof(Class1), myData.ToString());
return _Class1.GetType().GetProperty(propName).GetValue(_Class1, null).ToString();
}
else if (ActionClassName.Equals("TicketActionDef"))
{
_Class2 = new Class2();
_Class2 = (Class2)Class2.LoadFromXmlData(typeof(Class2), myData.ToString());
return __Class2.GetType().GetProperty(propName).GetValue(_Class2, null).ToString();
}
else
{
_Class3 = new Class3();
_Class3 = (Class1)Class1.LoadFromXmlData(typeof(Class3), myData.ToString());
return __Class3.GetType().GetProperty(propName).GetValue(_Class3, null).ToString();
}
}
I will load my DLL as below. How should I proceed after this. Please help, I am very new to reflection.
Assembly assembly = Assembly.GetAssembly(typeof(MyAdapter));
MyAdapter currentEventObject = (MyAdapter)assembly.CreateInstance(className);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不是 100% 确定我理解您想要实现的全部范围,但为了实例化命名类(确保包含完全限定名称;fullNamespace.className)并调用方法和属性您可以这样做:
这将使用要加载的类的类型和一些 xml 数据调用 LoadFromXmlData。
为了避免调用 LoadFromXmlData 方法的反射,您应该创建一个所有动态加载类型都必须实现的接口:
然后方法调用可以简化为:
如果您使用的是 C# 4,则可以使用dynamic 关键字来简化反思工作:
I'm not 100% sure I understand the full scope of what you're trying to achieve, but in order to instantiate a named class (make sure to include the fully qualified name; fullNamespace.className) and call a method and a property on it you can do it like this:
This will call the LoadFromXmlData with the type of the class to be loaded and some xml data.
In order to avoid the reflection for calling the LoadFromXmlData method, you should create an interface that all dynamically loaded types must implement:
Then the method call could be simplified into this:
If you are using C# 4, you could use the dynamic keyword to simplify the reflection work:
如果它是一个实例方法,你可以这样做
if it's an instance method, you can do
我相信您需要执行以下操作:
“Namespace.Class1”是程序集限定的。例如,类的程序集限定名称可能如下所示:
I believe you would need to do something like this:
"Namespace.Class1" is the assembly-qualified. For example, the assembly-qualified name for a class might look like this:
我认为您需要使用 Activator.CreateInstance() 方法创建该类的实例。并且可以直接在对象中调用该方法。
I think you need to create the instance of the class using Activator.CreateInstance() method. And you can call the method directly in the object.