有没有办法对通用类型对象进行扩展?
Is there a way to make an extension for a generic type object?
所以我试图破例通过扩展方法将我的 C# class 映射到原型对象它似乎不接受“this”的泛型类型?
public static ResponseReply ToProtoObject(this Response<T> reply) {
ResponseReply answer = new() {
Succes = reply.Success
};
if (reply.Object != null)
{
answer.ObjectId = reply.Object.Id.ToString();
}
if (reply.Messages != null)
{
answer.Messages.AddRange(reply.Messages.Select(x => x.ToProtoObject()));
}
return answer;
}
这些是响应
中的基础class
public class Response<T> where T : BaseObjectBo
{
public T Object { get; set; }
public bool Success { get; set; }
public List<Message> Messages { get; set; }
}
public class BaseObjectBo
{
public Guid Id { get; set; }
}
请注意,您只需将 <T>
添加到方法签名中即可。
public static ResponseReply ToProtoObject<T>(this Response<T> reply)
where T : BaseObjectBo
所以我试图破例通过扩展方法将我的 C# class 映射到原型对象它似乎不接受“this”的泛型类型?
public static ResponseReply ToProtoObject(this Response<T> reply) {
ResponseReply answer = new() {
Succes = reply.Success
};
if (reply.Object != null)
{
answer.ObjectId = reply.Object.Id.ToString();
}
if (reply.Messages != null)
{
answer.Messages.AddRange(reply.Messages.Select(x => x.ToProtoObject()));
}
return answer;
}
这些是响应
中的基础class public class Response<T> where T : BaseObjectBo
{
public T Object { get; set; }
public bool Success { get; set; }
public List<Message> Messages { get; set; }
}
public class BaseObjectBo
{
public Guid Id { get; set; }
}
请注意,您只需将 <T>
添加到方法签名中即可。
public static ResponseReply ToProtoObject<T>(this Response<T> reply)
where T : BaseObjectBo