返回一个类的对象在泛型方法 [英] Return an object of one class in a generic method

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

问题描述

我有一个场景,有两个类说ClassA和ClassB分别方法MethodA和MethodB。我必须写一个通用方法,返回上述类的对象实例,这取决于一些整数变量,x的条件。当我尝试下面的代码,我得到一个错误,不能隐式转换ClassA到T或不能隐式转换ClassB到T

  public ClassA 
{
public void MethodA()
{
//方法实现
}
}
$ b b public ClassB
{
public void MethodB()
{
//方法实现
}
}



通用方法为

  public T MethodGeneric< T>()
{
int x;
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();

if(x == 2)
{
return objectA;
}
else
{
return objectB;
}
}


解决方案

问题是 T 不是 ClassA 也不是 ClassB



您可以尝试通过投射处理此类问题,例如:

  public static T MethodGeneric< T>()其中T:class 
{
int x = 2;
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();

if(x == 2)
{
return objectA as T;
}
else
{
return objectB as T;
}
}

但是,如果有人调用 MethodGeneric< ClassC>(),除了在这种情况下返回 null



如果你有 ClassA ClassB 都来自同一个基类,接口,因为你可以放置一个通用约束,以减少错误的机会。然而,这仍然不是一个完全安全的工作方式,因为泛型将是不合适的。可能最好有两个类实现的接口(即: IClassBase ),然后不使用泛型,但返回接口:

  public IClassBase CreateInstance()
{
// ...
return objectA; //这将工作正常,提供ClassA实现IClassBase
}


I have a scenario where there are two classes say ClassA and ClassB with methods MethodA and MethodB respectively. I have to write generic method that returns the object instances of the above classes depending on the condition of some integer variable say x. When I try the code below, I get an error that says "Cannot implicitly convert ClassA to T" or "Cannot implicitly convert ClassB to T"

public ClassA
{
    public void MethodA()
    {
        //method implementation
    }
}

public ClassB
{
    public void MethodB()
    {
        //method implementation
    }
}

Generic method as

public T MethodGeneric<T>()
{
    int x;
    ClassA objectA = new ClassA();
    ClassB objectB = new ClassB();

    if(x==2)
    {
        return objectA;
    }
    else
    {
        return objectB;
    }
}

解决方案

The problem is that T is not ClassA nor ClassB.

You could attempt to handle this via casting, like so:

public static T MethodGeneric<T>() where T : class
{
    int x = 2;
    ClassA objectA = new ClassA();
    ClassB objectB = new ClassB();

    if (x == 2)
    {
        return objectA as T;
    }
    else
    {
        return objectB as T;
    }
}

However, this will not protect you if somebody calls MethodGeneric<ClassC>(), other than returning null in that case.

You could make this safer if you have ClassA and ClassB both derive from the same base class or interface, as you could then put a generic constraint in place that would reduce the chance of error. However, this would still not be a completely safe way of working, as the generics wouldn't be appropriate. It might be better to have an interface implemented by both classes (ie: IClassBase) and then not use generics, but return the interface:

public IClassBase CreateInstance()
{
    //... 
         return objectA; // This will work fine, provided ClassA implements IClassBase
}

这篇关于返回一个类的对象在泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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