Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回泛型方法中继承的类的接口_C#_Generics_Inheritance - Fatal编程技术网

C# 返回泛型方法中继承的类的接口

C# 返回泛型方法中继承的类的接口,c#,generics,inheritance,C#,Generics,Inheritance,我有3个物体非常相似,只有一些不同 public class Person { public Person(ResourceObject resource) { // resource comes from an API provided by one // of our systems (i have no control over it) this.ResourceObject = resource; } //

我有3个物体非常相似,只有一些不同

public class Person
{
    public Person(ResourceObject resource)
    {
        // resource comes from an API provided by one
        // of our systems (i have no control over it)
        this.ResourceObject = resource;
    }

    // Resource
    internal ResourceObject ResourceObject { get; }

    // Similar properties
    public string ObjectID { get; }
    public string ObjectType { get; }
    public IEnumerable<string> PropertyNames { get; }

    // Person-specific property example - Organisation
    public string Organisation { get; set; }
}

public class Computer
{
    public Computer(ResourceObject resource)
    {
        // resource comes from an API provided by one
        // of our systems (i have no control over it)
        this.ResourceObject = resource;
    }

    // Resource
    internal ResourceObject ResourceObject { get; }

    // Similar properties
    public string ObjectID { get; }
    public string ObjectType { get; }
    public IEnumerable<string> PropertyNames { get; }

    // Computer-specific property example - OperatingSystem
    public string OperatingSystem { get; set; }
}

public class Group
{
    public Group(ResourceObject resource)
    {
        // resource comes from an API provided by one
        // of our systems (i have no control over it)
        this.ResourceObject = resource;
    }

    // Resource
    internal ResourceObject ResourceObject { get; }

    // Similar properties
    public string ObjectID { get; }
    public string ObjectType { get; }
    public IEnumerable<string> PropertyNames { get; }

    // Group-specific property example - Members
    public string Members { get; set; }
}
然后尝试创建一个
GetResource
方法,但在返回代码处卡住了:

    public static T GetResource<T>(string identity) where T : IResource
    {
        // get resource from system API

        // and then return T somehow?
        return new T(resourceObject);
    }
public static T GetResource(字符串标识),其中T:IResource
{
//从系统API获取资源
//然后以某种方式返回T?
返回新的T(resourceObject);
}
我想将返回值从
T
更改为
IResource
,但我仍然不确定如何确定要返回哪个类(可能我需要一个基类?
Resource

对于这种特殊情况,我之所以选择泛型,是因为如果系统API更新后突然有了一个新的
Location
对象,我不想创建一个
GetLocation
方法,然后有4个方法,除了一行代码之外,它们做的事情完全相同


这是泛型的正确用例吗?如果是这样的话,我的方法如何确定要返回什么对象呢?

使用基类来保存公共行为

public abstract class Resource {

    protected Resource (ResourceObject resource) {
        // resource comes from an API provided by one
        // of our systems (i have no control over it)
        this.ResourceObject = resource;
    }

    // Resource
    internal ResourceObject ResourceObject { get; }

    // Similar properties
    public string ObjectID { get; }
    public string ObjectType { get; }
    public IEnumerable<string> PropertyNames { get; }
}
接口无法初始化,因此尝试传递构造函数参数无效

通过基类约束,泛型方法变为

public static T GetResource<T>(string identity) where T : Resource {
    // get resource from system API

    // and then return T somehow?
    return (T) Activator.CreateInstance(typeof(T), resourceObject);
}
publicstatict GetResource(字符串标识),其中T:Resource{
//从系统API获取资源
//然后以某种方式返回T?
return(T)Activator.CreateInstance(typeof(T),resourceObject);
}
使用

Person person = GetResource<Person>("person_identity");
Person-Person=GetResource(“Person_-identity”);

Whoa,
Activator.CreateInstance
非常酷,以后我必须记住这一点。如您的答案所示,使用基类+
Activator.CreateInstance
非常有效。谢谢你抽出时间来帮助我。:)
public static T GetResource<T>(string identity) where T : Resource {
    // get resource from system API

    // and then return T somehow?
    return (T) Activator.CreateInstance(typeof(T), resourceObject);
}
Person person = GetResource<Person>("person_identity");