类的实例 [英] Instance of class

查看:62
本文介绍了类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道是否存在一种使用变量"myClass"实例化类并调用方法并始终通过变量在变量内部呈现变量的方法.示例:

Hi, I wanted to know if exists up a way to instantiate a class using a variable "myClass" and invoke a method and present a variable inside a variable always through. Example:

myClassName string;
metodName string;
string variableName;
For the class:
Assembly a = Assembly.Load ("MyLibrary");
Type t = a.GetType (MyLibrary.MyClass ");
object obj = Activator.CreateInstance (t);
For the method and variable?
t.xxxxxx (metodName)
t.xxxxxx (variableName)

thanks

推荐答案

使用Google搜索按名称实例化c#类"(以及超过95,000个其他结果):

http://www.csharphelp.com/archives/archive200.html [
Googling "c# instantiate class by name" would have led you here (as well as over 95,000 other results):

http://www.csharphelp.com/archives/archive200.html[^]


类型为Type的对象具有一个方法InvokeMember.这就是您要寻找的.查看有关如何使用它的MSDN.
An object of class Type has a method InvokeMember. That is what you are looking for. Check out MSDN on how to use it.


如何使用.Net 4.0中引入的动态语言运行库?

这样,您无需调用任何东西即可访问其类型对于编译器未知的对象的成员.

或者...

让MyClass实现IMyPluginInterface-接口在其中声明应公开的成员-然后:

How about using the Dynamic Language Runtime introduced in .Net 4.0?

That way you don''t have to invoke anything to access members of an object whose type is unknown to the compiler.

Or ...

Have MyClass implement IMyPluginInterface - where the interface declares the members that should be exposed - then:

IMyPluginInterface someObject = (IMyPlugInInterface)Activator.CreateInstance("MyLibrary", "MyClass");
someObject.SomeMethod();
...


这篇关于类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆