Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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_Types_.net Core - Fatal编程技术网

C#-匹配类型和值的最有效方法

C#-匹配类型和值的最有效方法,c#,generics,types,.net-core,C#,Generics,Types,.net Core,我正在开发EVE在线应用程序。EVE API提供了不同的数据,我想为所有这些数据编写通用方法 我的想法是编写泛型方法,从传递的类型中“提取”到API的路径,并发送适当的请求。例如: interface ICommonResponse { //some method or property } class FwLeaderboards : ICommonResponse { //some fields corresponding to api response } public

我正在开发EVE在线应用程序。EVE API提供了不同的数据,我想为所有这些数据编写通用方法

我的想法是编写泛型方法,从传递的类型中“提取”到API的路径,并发送适当的请求。例如:

interface ICommonResponse
{
    //some method or property
}

class FwLeaderboards : ICommonResponse
{
    //some fields corresponding to api response
}

public T Get<T>() where T : ICommonResponse
{
    string path = SomeMagic(T); //we "extract" path from type
    return Send<T>(path); //this method retrieves JSON string from api and deserializes it to object of FwLeaderboards type
}
实例化 我们创建类的实例并调用方法

interface ICommonResponse
{
    string Path() => null;
}

class FwLeaderboards : ICommonResponse
{
    public override string Path() => "/fw/leaderboards/";
    //some fields corresponding to api response
}

public T Get<T>() where T : ICommonResponse, new()
{
    T val = new T();
    return Send<T>(val.Path());
}

那么,最有效的方法是什么?

在我的客户服务中,
get
方法的确切位置是什么?@Progressive。这没关系,响应类既不是嵌套的,也不是私有的。您可以通过ICommonResponse对象来获取方法并直接到达
.Path
。撰写
ICommonResponse
非常适合您的情况。或者您知道,您可以创建一个
ICommonRequest
我不会将请求url存储在响应对象中,因为它在那里感觉不合适(url用于请求,而不是响应)。此外,有些请求还需要其他参数,因此您需要知道提交什么。另外,看看ESI.NET、EVEStandard或其他Eve在线库可能会为您节省一些时间,您可以自己重新实现API。
get
方法具体位于哪里?@Progressive在我的EveClient服务中。这没关系,响应类既不是嵌套的,也不是私有的。您可以通过ICommonResponse对象来获取方法并直接到达
.Path
。撰写
ICommonResponse
非常适合您的情况。或者您知道,您可以创建一个
ICommonRequest
我不会将请求url存储在响应对象中,因为它在那里感觉不合适(url用于请求,而不是响应)。此外,有些请求还需要其他参数,因此您需要知道提交什么。另外,看看ESI.NET、EVEStandard或其他Eve在线库可能会为您自己重新实现API节省一些时间。
interface ICommonResponse
{
    string Path() => null;
}

class FwLeaderboards : ICommonResponse
{
    public override string Path() => "/fw/leaderboards/";
    //some fields corresponding to api response
}

public T Get<T>() where T : ICommonResponse, new()
{
    T val = new T();
    return Send<T>(val.Path());
}
IDictionary<Type, string> dict = ...;//Initialize dictionary

interface ICommonResponse
{
}

class FwLeaderboards : ICommonResponse
{
    //some fields corresponding to api response
}

public T Get<T>() where T : ICommonResponse
{
    var path = dict[typeof(T)]; //returns "/fw/leaderboards/" for FwLeaderboards
    return Send<T>(val.Path());
}