C#泛型和重写方法 [英] C# Generic and override methods

查看:429
本文介绍了C#泛型和重写方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个问题,意识到这是由于我方面的一些设计错误.但是,我确实认为该概念可能有用,并且我想知道如果可能的话,如何解决类似的问题.

I created a question and realized that it was due to some design error on my part. However, I do think that that concept can be useful and I would like to know how to solve an issue similar, if possible.

我想创建一个将两个数字相加的算法,无论是string还是int.我还希望能够在控制台中输出所有内容,并希望避免代码重复.我创建了一个generic interface,它会根据模型而变化.然后是一个处理两个成员加法的类.请注意,数据来自处理程序类(在现实生活中,该类从外部数据获取信息以填充模型).

I want to create an algorithm that adds up 2 numbers, be it string or int. I also want to be able to output everything in the console and would like to avoid code duplication. I create a generic interface that changes according to the model. Then a class that handles the addition of the two members. Note that the data comes from the handler classes (which in real life scenario, get information to fill model from external data).

请注意,我确实知道有很多更简单的掺杂方法,但是我想了解为什么它不起作用.现在,该代码无法在由于cannot convert from 'T' to 'Test.Models.StringModel'.为什么不能选择正确的覆盖方法?我尝试将泛型添加到重写的方法中,但仍然无法正常工作.如何使ValidateHandler<T>从其类型中选择正确的方法?

Please note that I do know that I know there are much simpler ways of doping this, but I want to understand why it does not work. Now, the code does not compile in the ValidateHandler<T>(IAddHandler<T> handler) due to cannot convert from 'T' to 'Test.Models.StringModel'. Why cannot it select the right overridden method ? I tried adding generics to the overridden method, but it still does not work. How can I make ValidateHandler<T> select the right method from it's type ?

这是我写的代码.

型号:

public class IntegerModel
{
    public int A { get; set; }

    public int B { get; set; }

    public int C { get; set; }
}

public class StringModel
{
    public string A { get; set; }

    public string B { get; set; }

    public string C { get; set; }
}

接口:

public interface IAddHandler<T>
{
    T Add();

    void GetData();
}

处理程序:

public class IntegerHandler : IAddHandler<IntegerModel>
{
    public IntegerModel IntegerModel { get; set; }

    public void GetData()
    {
        // Get Info to Add from external file for example
        IntegerModel = new IntegerModel { A = 10, B = 20 };
    }

    public IntegerModel Add()
    {
        IntegerModel.C = IntegerModel.A + IntegerModel.B;

        return IntegerModel;
    }
 }

public class StringHandler : IAddHandler<StringModel>
{
    public StringModel StringModel { get; set; }

    public void GetData()
    {
        // Get Info to Add from external file for example
        StringModel = new StringModel { A = "10", B = "20" };
    }

    public StringModel Add()
    {
        StringModel.C = StringModel.A + StringModel.B;
        return StringModel;
    }
 }

这里是主要功能

public static void Main(string[] args)
{
    var integerHandler = new IntegerHandler();
    var stringHandler = new StringHandler();

    ValidateHandler(integerHandler);
    ValidateHandler(stringHandler);
}

public static void ValidateHandler<T>(IAddHandler<T> handler)
{
    handler.GetData();
    var result = handler.Add();

    WriteResults(result);
}

public static void WriteResults(StringModel model)
{
    Console.WriteLine(model.C);
}

public static void WriteResults(IntegerModel model)
{
    Console.WriteLine(model.C);
}

我知道我可以执行以下操作,但是这样做似乎无效,因此我看不到使用泛型的意义.

I know I can do the following, but it seems ineffective in doing so and I do not see the point of using generics then.

public static void ValidateHandler<T>(IAddHandler<T> handler)
{
    handler.GetData();
    var result = handler.Add();

    if (typeof(T) == typeof(StringModel))
    {
        WriteResults(result as StringModel);
    }
    else if (typeof(T) == typeof(IntegerModel))
    {
        WriteResults(result as IntegerModel);
    }
}

推荐答案

我将编写一个使用object而不是type参数的非通用IModel接口,并在您的通用类中明确实现它.

I'd write a non-generic IModel interface which uses object instead of the type parameter, and implement it explicitly in your generic class.

这篇关于C#泛型和重写方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆