如何传递一个类作为参数的方法? [英] How to pass a Class as parameter for a method?
问题描述
我有两个班级:
Class Gold;
类函数;
类中有一个方法 ClassGet
code>函数,它有2个参数。
我想发送类 Gold
作为类 Functions
中的一个方法的参数。
怎么可能?
例如:
public void ClassGet(class MyClassName,string blabla)
{
MyClassName NewInstance = new MyClassName();
}
注意:我想发送 MyClassName
作为我的方法的字符串参数。
实现已经存在(有点不同)
查看Activator类: http://msdn.microsoft.com/en-us/library/system.activator.aspx
<例如:
object instance = Activator.Create(className);
或者像这样:
Type type = typeof(MyClass);
MyClass instance =(MyClass)Activator.Create(type);
或您的情况:
public void ClassGet(string MyClassName,string blabla)
{
object instance = Activator.Create(MyClassName);
}
//称之为:
Gold g = new Gold();
g.ClassGet(MyClass,blabla);
I have two classs:
Class Gold;
Class Functions;
There is a method ClassGet
in class Functions
, which has 2 parameters.
I want to send the class Gold
as parameter for one of my methods in class Functions
.
How is it possible?
For example:
public void ClassGet(class MyClassName, string blabla)
{
MyClassName NewInstance = new MyClassName();
}
Attention: I want to send MyClassName
as string parameter to my method.
The function you're trying to implement already exists (a bit different)
Look at the Activator class: http://msdn.microsoft.com/en-us/library/system.activator.aspx
example:
object instance = Activator.Create(className);
Or like this:
Type type = typeof(MyClass);
MyClass instance = (MyClass)Activator.Create(type);
or in your case:
public void ClassGet(string MyClassName,string blabla)
{
object instance = Activator.Create(MyClassName);
}
// Call it like:
Gold g = new Gold();
g.ClassGet("MyClass", "blabla");
这篇关于如何传递一个类作为参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!