简体   繁体   English

C# 通用和覆盖方法

[英]C# Generic and override methods

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.但是,我确实认为这个概念很有用,如果可能的话,我想知道如何解决类似的问题。

I want to create an algorithm that adds up 2 numbers, be it string or int .我想创建一个将 2 个数字相加的算法,无论是string还是int I also want to be able to output everything in the console and would like to avoid code duplication.我还希望能够在控制台中进行 output 的所有操作,并希望避免代码重复。 I create a generic interface that changes according to the model.我创建了一个根据 model 更改的generic interface Then a class that handles the addition of the two members.然后是处理两个成员的添加的 class。 Note that the data comes from the handler classes (which in real life scenario, get information to fill model from external data).请注意,数据来自处理程序类(在现实生活场景中,从外部数据获取信息以填充 model)。

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' .现在,由于cannot convert from 'T' to 'Test.Models.StringModel' ,代码无法在ValidateHandler<T>(IAddHandler<T> handler)中编译。 Why cannot it select the right overridden method?为什么不能 select 正确的覆盖方法? I tried adding generics to the overridden method, but it still does not work.我尝试将 generics 添加到覆盖的方法中,但它仍然不起作用。 How can I make ValidateHandler<T> select the right method from it's type?如何使ValidateHandler<T> select 从它的类型成为正确的方法?

Here is the code I wrote.这是我写的代码。

Models:楷模:

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; }
}

Interface:界面:

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

    void GetData();
}

Handlers:处理程序:

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;
    }
 }

Here is the Main with it's function这是主要的 function

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.我知道我可以执行以下操作,但是这样做似乎无效,并且我看不到使用 generics 的意义。

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);
    }
}

I'd write a non-generic IModel interface which uses object instead of the type parameter, and implement it explicitly in your generic class.我会编写一个使用object而不是类型参数的非通用 IModel 接口,并在通用 class 中显式实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM