I am new to creating generic methods and have come across casting issues.
我是创建泛型方法的新手,遇到过强制转换问题。
The following is a class library for communicating with an API. The object I am trying to cast to lives in my main application, which might be the issue but I do not know. What might I be doing wrong?
下面是用于与API通信的类库。我试图在我的主应用程序中将对象转换为life,这可能是问题所在,但我不知道。我做错了什么?
public T GetRequest<T>(Authentication auth, string api)
{
var restClient = new RestClient(Constants.Api.Client);
var restRequest = new RestRequest(api, Method.GET);
SetParameters(restRequest, auth);
var restResponse = restClient.Execute(restRequest);
return JsonConvert.DeserializeAnonymousType<T>(restResponse.Content, (T)new object());
}
2 个解决方案
#1
2
The object I am trying to cast to lives in my main application
在我的主要应用程序中,我试图将对象转换为life
It's not entirely clear what you mean by that, but the object you're trying to cast is just an instance of System.Object
:
我不太清楚你的意思,但你要投射的对象只是System.Object的一个例子。
(T)new object()
That can never work unless T
itself is object
.
除非T本身是物体,否则它永远不会工作。
The problem here appears to be that you're trying to use a method designed for working with anonymous types (so the second parameter is present as an "example" to make type inference work) - but with a type which isn't anonymous (T
).
这里的问题似乎是您正在尝试使用一种设计用于处理匿名类型的方法(因此第二个参数作为“示例”出现以使类型推断工作)——但是使用的类型不是匿名的(T)。
I suspect you just want:
我猜你只是想:
return JsonConvert.DeserializeObject<T>(restResponse.Content);
#2
0
In your method last line (T)new object()
is a problem. This makes no sense. You're creating new instance of System.Object
and trying to cast it to T
where T
could be anything. Your method is going to succeed only when called with generic parameter T
as System.Object
. For example GetRequest<object>(...)
will pass, otherwise it will fail.
在您的方法中,最后一行(T)new object()是一个问题。这没有任何意义。您正在创建系统的新实例。对象并试着将它转换为T,其中T可以是任何东西。只有用泛型参数T作为System.Object调用时,您的方法才会成功。例如GetRequest
Am not sure what you're trying to achieve here. If you can provide more info it will be easy to help.
我不确定你想在这里实现什么。如果你能提供更多的信息,这将很容易帮助。
#1
2
The object I am trying to cast to lives in my main application
在我的主要应用程序中,我试图将对象转换为life
It's not entirely clear what you mean by that, but the object you're trying to cast is just an instance of System.Object
:
我不太清楚你的意思,但你要投射的对象只是System.Object的一个例子。
(T)new object()
That can never work unless T
itself is object
.
除非T本身是物体,否则它永远不会工作。
The problem here appears to be that you're trying to use a method designed for working with anonymous types (so the second parameter is present as an "example" to make type inference work) - but with a type which isn't anonymous (T
).
这里的问题似乎是您正在尝试使用一种设计用于处理匿名类型的方法(因此第二个参数作为“示例”出现以使类型推断工作)——但是使用的类型不是匿名的(T)。
I suspect you just want:
我猜你只是想:
return JsonConvert.DeserializeObject<T>(restResponse.Content);
#2
0
In your method last line (T)new object()
is a problem. This makes no sense. You're creating new instance of System.Object
and trying to cast it to T
where T
could be anything. Your method is going to succeed only when called with generic parameter T
as System.Object
. For example GetRequest<object>(...)
will pass, otherwise it will fail.
在您的方法中,最后一行(T)new object()是一个问题。这没有任何意义。您正在创建系统的新实例。对象并试着将它转换为T,其中T可以是任何东西。只有用泛型参数T作为System.Object调用时,您的方法才会成功。例如GetRequest
Am not sure what you're trying to achieve here. If you can provide more info it will be easy to help.
我不确定你想在这里实现什么。如果你能提供更多的信息,这将很容易帮助。