gpt4 book ai didi

c# - 在通用方法中返回接口(interface)继承的类

转载 作者:行者123 更新时间:2023-11-30 15:54:02 24 4
gpt4 key购买 nike

我有 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; }
}

我目前有GetPerson , GetComputerGetGroup有效的方法,但它们本质上做同样的事情,然后调用特定对象构造函数之一。为了深入研究泛型和接口(interface)的世界并了解更多(就像你一样),我尝试创建一个 GetResource<T>方法可以完成与这 3 种方法相同的工作,而无需所有重复代码。

我创建了 IResource识别公共(public)属性的接口(interface):

public interface IResource
{
string ObjectID { get; }
string ObjectType { get; }
IEnumerable<string> PropertyNames { get; }
}

然后尝试创建一个 GetResource<T>方法,但卡在了返回码处:

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

// and then return T somehow?
return new 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 class Person : Resource {

public Person(ResourceObject resource):base(resource){
}

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

public class Computer : Resource {
public Computer(ResourceObject resource) : base(resource) {
}

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

public class Group : Resource {

public Group(ResourceObject resource) : base(resource) {
}

// Group-specific property example - Members
public string Members { get; set; }
}

接口(interface)无法初始化,因此尝试传递构造函数参数是行不通的。

有了基类约束,泛型方法就变成了

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");

关于c# - 在通用方法中返回接口(interface)继承的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51434966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com