按类类型引用创建对象? [英] Create an object by class Type reference?
问题描述
我有一堆来自同一基类的对象。他们都与一些参数共享
相同的构造函数。现在,我没有使用一个大的
switch()语句,而是我自己调用每个Object,我想
分配对类类型的引用,在不知道
派生类型的情况下稍后调用它。
示例:
类BaseA {
public BaseA(int aParam){...}
}
class DerivedA {
public DerivedA( int aParam):BaseA(aParam){...}
}
class DerivedB {
public DerivedB(int aParam ):BaseA(aParam){...}
}
现在
东西a = DerivedA ;
开关(类型)
{
1:a = DerivedA;休息;
2:a = DerivedB;休息;
}
BaseA实例=新a(5);
这是可能?实际的参数列表很长,所以稍后再做它会很酷。
更好:我可以创建按字符串输入类型(由于某种反思
功能或其他东西)?
谢谢
Doc
docschnipp写道:
我有一堆从同一基类派生的对象。他们都与一些参数共享
相同的构造函数。现在,我没有使用一个大的
switch()语句,而是我自己调用每个Object,我想
分配对类类型的引用,在不知道
派生类型的情况下稍后调用它。
示例:
类BaseA {
public BaseA(int aParam){...}
}
class DerivedA {
public DerivedA( int aParam):BaseA(aParam){...}
}
class DerivedB {
public DerivedB(int aParam ):BaseA(aParam){...}
}
现在
东西a = DerivedA ;
开关(类型)
{
1:a = DerivedA;休息;
2:a = DerivedB;休息;
}
BaseA实例=新a(5);
这是可能?实际的参数列表很长,所以稍后再做它会很酷。
更好:我可以创建按字符串输入类型(由于一些反思
功能或其他东西)?
谢谢
Doc
完成此任务的最佳方法是反射和Activator类:
class BaseA {
public BaseA(int aParam,double s){}
}
类DerivedA:BaseA {
public DerivedA(int aParam,double s):base(aParam,s){} < br $>
}
类DerivedB:BaseA {
public DerivedB(int aParam,double s):base(aParam,s){ }
}
class Program
{
static void Main()
{
//从字符串中获取类型
类型derivedAType =
Type.GetType(
" ConsoleApplication.DerivedA,ConsoleApplication");
类型derivedBType = typeof(DerivedB);
//按类型创建实例:
BaseA a1 =
(BaseA)Activator.CreateInstance(derivedAType,10, 2.0);
BaseA a2 =
(BaseA)Activator.CreateInstance(derivedBType,10,10.0);
}
}
}
HTH,
Andy
-
您可以通过删除下面的NOSPAm直接给我发电子邮件
xm ******* ***@gmxNOSPAm.netN OSPAm
" Andreas Mueller"写道:
完成此任务的最佳方法是反射和Activator类:
...
谢谢。这正是我试图找到的。我猜,GetType()函数
如果该类不存在则返回null,对吧?
再次感谢这个非常好的例子。
doc
docschnipp写道:
" Andreas Mueller"写道:
>>完成此任务的最佳方法是反射和Activator类:
...
谢谢。这正是我试图找到的。我猜,GetType()函数
如果该类不存在则返回null,对吧?
是的,确实如此。
>
再次感谢非常好的例子。
doc
-
您可以通过删除下面的NOSPAm直接给我发电子邮件
xm**********@gmxNOSPAm.netN OSPAm
Hi,
I have a bunch of object derived from the same base class. They all share
the same constructor with some parameters. Now, instead of using a large
switch() statement where I call every single Object by itself, I''d like to
assign a reference to the class type and call it later without knowing the
derived type.
Example:
class BaseA {
public BaseA (int aParam) {...}
}
class DerivedA {
public DerivedA(int aParam) : BaseA (aParam){ ... }
}
class DerivedB {
public DerivedB(int aParam) : BaseA (aParam){ ... }
}
and now
Something a = DerivedA;
switch(type)
{
1: a = DerivedA; break;
2: a = DerivedB; break;
}
BaseA instance = new a(5);
Is this possible? The actual parameter list is very long, so it would be
cool to do it later.
Even better: can I create get the Type by string (due some reflection
feature or something)?
Thanks
Doc
docschnipp wrote:Hi,
I have a bunch of object derived from the same base class. They all share
the same constructor with some parameters. Now, instead of using a large
switch() statement where I call every single Object by itself, I''d like to
assign a reference to the class type and call it later without knowing the
derived type.
Example:
class BaseA {
public BaseA (int aParam) {...}
}
class DerivedA {
public DerivedA(int aParam) : BaseA (aParam){ ... }
}
class DerivedB {
public DerivedB(int aParam) : BaseA (aParam){ ... }
}
and now
Something a = DerivedA;
switch(type)
{
1: a = DerivedA; break;
2: a = DerivedB; break;
}
BaseA instance = new a(5);
Is this possible? The actual parameter list is very long, so it would be
cool to do it later.
Even better: can I create get the Type by string (due some reflection
feature or something)?
Thanks
Doc
The best way to accomplish this is reflection and the Activator class:
class BaseA{
public BaseA (int aParam, double s) {}
}
class DerivedA : BaseA{
public DerivedA(int aParam, double s) : base(aParam, s) { }
}
class DerivedB : BaseA{
public DerivedB(int aParam, double s) : base(aParam, s) { }
}
class Program
{
static void Main()
{
// get a type from a string
Type derivedAType =
Type.GetType(
"ConsoleApplication.DerivedA, ConsoleApplication");
Type derivedBType = typeof (DerivedB);
// create the instances by type:
BaseA a1 =
(BaseA) Activator.CreateInstance(derivedAType, 10, 2.0);
BaseA a2 =
(BaseA)Activator.CreateInstance(derivedBType, 10, 10.0);
}
}
}
HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
"Andreas Mueller" wrote:
The best way to accomplish this is reflection and the Activator class:
...Thanks. That is exactly what I tried to find. I guess, the GetType() function
returns null if the class does not exist, right?
Thanks again for the very good example.
doc
docschnipp wrote:
"Andreas Mueller" wrote:
>>The best way to accomplish this is reflection and the Activator class:
...
Thanks. That is exactly what I tried to find. I guess, the GetType() function
returns null if the class does not exist, right?Yes, it does.
>
Thanks again for the very good example.
doc
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
这篇关于按类类型引用创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!