在C#中返回的实例使用反射 [英] Return instance using reflection in C#

查看:149
本文介绍了在C#中返回的实例使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个样本code我试图返回类的一个实例如下。

A sample code I tried to return an instance of class is given below.

public object getConstructorclass(int i)
{  
    if(i==1)
    {
       Type type = Type.GetType("test1");
    }else
    {
       Type type = Type.GetType("test2");
    }
    return Activator.CreateInstance(type); 
}    

var objcls = getConstructorclass(1);
objcls.callclass();//error occured

我怎么能在这里提到的类类型,因为该类型无法在编译时已知的,但它将在runtime.In决定上面的例子中,我传递一个值1(它可以是任何东西,则该类将相应的调用) ,类test1的调用。

How can I mention the class type here since the type is not known at compile time but it will decided at runtime.In the above example i just pass a value 1 (it can be anything and that class will be called accordingly), and the class test1 called.

我将上线得到一个错误 objcls.callclass(),因为 objcls 对象实例不具有 callclass()方法。

here I will get an error on the line objcls.callclass(), because objcls is an object instance that doesn't have a callclass()method.

我怎么能重组这块code的?我的目标是,如果我提到在 getConstructorclass()方法的类,一个对象应该返回,以便使用它在进一步code调用成员该类。

How can I restructure this piece of code? My aim is if I mention a class in the getConstructorclass() method, an object should be returned so as to use it in the further code to invoke the members of that class.

推荐答案

如果你知道你的班级都会有这样的方法,你应该使用他们的一个共同的界面和遵照执行。然后,你将用你已经确定它会工作类的工作。

If you know that your classes will have this method, you should use a common interface for them and implement it accordingly. Then you will work with classes that you have made sure it will work.

它看起来像这样

IMyInterface objcls = getconstrorclass() as IMyInterface;
if (objcls != null)
    objcls.callclass();
else
    // we failed miserably and should do something about it

我不认为你应该使用基于int变量一些通用的对象返回构造函数,如果你的类没有什么共同点。这很古怪来处理像这样,它可能导致各种问题(其中一些您目前已经出现了)。通用类的构造函数意义,如果这些类是有些关联,您可以predict的结果,而是创造一个做它,所有的方法..不这么肯定这种做法的正确性。

I don't think you should use some generic object returning constructor based on an int variable, if your classes don't have anything in common. It's really weird to handle it like this and it may lead to various problems (some of which you're currently already experiencing). Generic class constructors make sense if the classes are somewhat related and you can predict the outcome, but to create a do-it-all method.. Not so sure about correctness of such approach.

无论如何,如果你坚持(不推荐,但你愿意的话),你可以为一个类型是这样创造了一些检查:

Anyway, if you insist (not recommended, but as you wish), you can create some checks for a type like this:

var createdObject = getConstructorclass(1);
if (createdObject is MyClass1)
{
    var specificObject = (MyClass1)createdObject;
    specificObject.callMethod1(); 
}
else if (createdObject is MyClass2)
{
    var specificObject = (MyClass2)createdObject;
    specificObject.callSomeOtherMethod();
}
...

但它很快就容易变得非常错误,重构可能会是一场噩梦等,但它是你的电话。

But it gets very error prone soon, refactoring will probably be a nightmare etc., but it's your call..

或者,你也许可以使用来自PWAS的解决方案,但对我来说,似乎对于这样的基本任务过于复杂。看起来不错,所有的,但它仍然只返回类型的对象,所以它并没有真正解决您的具体问题。

Or you maybe can use solution from pwas, but to me it seems unnecessarily complicated for such a basic task. Looks nice and all, but it still returns only the type "object", so it doesn't really solve your specific problem.

此外,为了解决一个问题,我不知道你明白 - 你已经创建的实例,你可以返回类型的对象。这就是为什么你不能调用这个对象上的任何具体的方法,因为首先你必须将它转换的东西,实际上有一个方法,并确保铸件可以做到(继承等)。

Also, to address one issue I'm not sure you understand - you've already created the instance, you just return type object. That is why you can't call any specific methods on this object, because first you have to cast it to something, that actually has that method and make sure the cast can be done (inheritance etc).

这篇关于在C#中返回的实例使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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