[英]c# Generic property
I am trying to make a class in C# that can be used to return data of any types. 我试图在C#中创建一个可用于返回任何类型数据的类。
public class ResponseObject
{
public <T> data { get;set }
public Boolean Success { get; set; }
public string Message { get; set; }
}
The Object will be a wrapper for the response object when my application sends a request to the API. 当我的应用程序向API发送请求时,对象将是响应对象的包装。
i have tried researching this but cannot find any tutorials which are relevant to what i am trying to do. 我已经尝试过对此进行研究,但找不到与我正在尝试相关的任何教程。
Is this possible in C#? 这在C#中可能吗? the Response Object will be converted to a JSON string and then sent as a response. 响应对象将转换为JSON字符串,然后作为响应发送。
I will not be doing any processing of this object as that will already by done. 我不会对此对象进行任何处理,因为已经完成了。 I just want to place the data inside the ResponseObject and send it 我只想将数据放在ResponseObject内并发送
I want to do something along the lines of: 我想按照以下方式做一些事情:
var customers = repository.GetCustomers();
var orders = repository.GetOrders();
if(customers)
{
success = true;
message = "";
}
else{
success = false;
message = "failed to get customers";
}
if(orders)
{
orderssuccess = true;
ordersmessage = "";
}
else{
orderssuccess = false;
ordersmessage = "failed to get orders";
}
ResponseObject customerResponse = new ResponseObject{
data = customers,
success = success,
message = message
};
ResponseObject orderResponse = new ResponseObject{
data = orders,
success = orderssuccess,
message = ordersmessage
};
You need to add <T> to the class and use T as the type of your property. 您需要将<T>添加到类中,并使用T作为属性的类型。
public class ResponseObject<T>
{
public T data { get; set; }
public Boolean Success { get; set; }
public string Message { get; set; }
}
You have almost done it already! 您已经快完成了! Just change your <T>
to T
. 只需将<T>
更改为T
。
public class ResponseObject<T> where T : class
{
public T data { get; set; }
public Boolean Success { get; set; }
public string Message { get; set; }
}
Here where T : class
ensure that the generic type parameter is a reference type. 在where T : class
确保泛型类型参数是引用类型。 From your question it seems you are going to pass in an object there. 从您的问题看来,您似乎要在此处传递一个对象。
You have two options here: 您在这里有两个选择:
Here are the examples of both approaches: 以下是这两种方法的示例:
// Make the class generic
public class ResponseObject<T> {
public T Data { get; set }
public Boolean Success { get; set; }
public string Message { get; set; }
}
// Use generic methods to access the property
public class ResponseObject {
private object data;
public T GetData<T>() {
return (T)data;
}
public void SetData<T>(T newData) {
data = newData;
}
public Boolean Success { get; set; }
public string Message { get; set; }
}
Second approach is less robust than the first one - basically, it's a glorified unrestricted cast. 第二种方法不如第一种稳健-基本上,这是一种光荣的,不受限制的演员表。 First approach, however, does not let you build a container of ResponseObject
s with different T
s. 但是,第一种方法不允许您构建具有不同T
的ResponseObject
的容器。 You can address this problem by adding an interface on top of ResponseObject
: 您可以通过在ResponseObject
顶部添加接口来解决此问题:
interface IResponseObject {
object DataObj { get; set }
Type ObjType { get; }
bool Success { get; set; }
string Message { get; set; }
}
public class ResponseObject<T> {
public T Data { get; set }
public ObjType => typeof(T);
public DataObj {
get => Data;
set => Data = value; // C# 7 syntax
}
public Boolean Success { get; set; }
public string Message { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.