1

有一个从非泛型类继承的泛型类,如下结构:

public class Result
{
     public string ErrorCode { get; set;}
     public string ErrorMessage { get; set;}
     public boo Success { get; set;}
     //Lots more properties

     public ClientResult ToClientResult()
     {
         //some pretty involved calculations of error coded and status
     }
 }

 public class Result<T> : Result
 {
     public T details {get; set;}

     public ClientResult<T> ToClientResult<T>()
     {
        //Need to call the parent class implementation and convert result to  generic ver  
     }
 }

我的问题是如何调用父ToClientResult()级并将结果转换为通用版本,ClientResult<T>然后我需要将属性设置ClientResult<T>为类的详细信息属性Result<T>

我确定我在这里缺少一个简单的解决方案,我真的不想复制父类逻辑,因为它非常复杂。

4

3 回答 3

2

如果将父类型的对象创建为父类型(使用new ClientResult()),则不能将其强制转换为子类型。它只是不那样工作。

Result您可以做的是将复杂的代码分解到另一种方法中,用于在类和类中进行繁重的工作Result<T>

public class Result
{
    public string ErrorCode { get; set;}
    public string ErrorMessage { get; set;}
    public boo Success { get; set;}
    //Lots more properties

     public ClientResult ToClientResult()
     {
         var clientResult = new ClientResult();
         SetupClientResult(clientResult);
         return clientResult;
     }

     protected void SetupClientResult(ClientResult clientResult) 
     {    
         //some pretty involved calculations of error coded and status           
     }

}

public class Result<T> : Result
{
     public T details {get; set;}

     // This now shadows the original ToClientResult method. The trap here is that if
     // you are treating your Result<T> instance as a Result, this method will not be 
     // called, and the return type will be ClientResult and not ClientResult<T>.
     // See: http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c?lq=1
     public ClientResult<T> ToClientResult()
     {
         var clientResult = new ClientResult<T>();
         SetupClientResult(clientResult);

         clientResult.SomeProperty = details;

         return clientResult;
     }
}

这一切都假设ClientResult<T>源自ClientResult,从您的问题中很难说出来。

于 2013-01-30T09:22:54.133 回答
0

ClientResult<T>课堂上,您可以进行自定义转换,从 ClientResult 到ClientResult<T>

public static explicit operator ClientReault<T>(ClientResult result)
{
       //do your conversion from one to the other here
}

然后你可以这样写 ToClientResult

 //Generic argument remove from method declaration
 //because it was shadowing the type argument
 public ClientResult<T> ToClientResult()
 {
    var clientResult = ((Result)this).ToClientResult()
    var genericResult = (ClientResult<T>)clientResult;
    //do what you need to do with the generically typed object
    //...
    return genericResult
 }

也就是说,当继承链中存在缺陷时,通常会出现这种情况。例如,当基类和派生类之间没有 is-a 关系时

于 2013-01-30T09:32:18.663 回答
0

您应该明确地进行转换:

    public class ClientResult
    {
        public int a {get;set;}
    }

    public class ClientResult<T> : ClientResult
    {
        public ClientResult(ClientResult cr)
        {
            this.a = cr.a;
        }
    }

    public class Result<T> : Result
    {
        public T details { get; set; }

        public ClientResult<T> ToClientResult<T>()
        {
            var cr = base.ToClientResult();
            return new ClientResult<T>(cr);  
        }
    }

如果你有很多类似于 ClientResult 的类,你可以使用像AutoMapper这样的工具

于 2013-01-30T09:41:31.920 回答