繁体   English   中英

泛型类型的c#调用方法

[英]c# Call method for generic types

想法如下:我想要一种增加项目价值的机制。 项目可以是bool,int,double。 每种类型都应实现其自己的“反向”逻辑。

逻辑是这样的:

    public class Integers : IInterface
{
    public int Reverse(int value)
    {
        return -value;
    }
}

public class Bools : IInterface
{
    public bool Reverse(bool value)
    {
        return !value;
    }
}

我知道对于泛型的所有类都必须从sth common派生,因此我创建了一个称为IInterface的接口:

    public interface IInterface<T>
{
    T Reverse(T value);
}

现在在我的代码中,我想像这样运行:

Reverse(1); //int - expect to return -1
Reverse(true); // bool - expect to return false

我被困住了,不知道如何做到这一点。

有人告诉我,包装所有原始类型以实现此类方法不是一个好主意,从长远来看,您将面临不必要的复杂性。 为什么不使用扩展方法 例:

namespace MyNamepace
{
    public static class Program
    {
        public static void Main(String[] args)
        {
            Boolean b = true;
            Boolean b_rev = b.Reverse();
            Console.WriteLine(b); // True
            Console.WriteLine(b_rev); // False

            Int32 i = -20;
            Int32 i_rev = i.Reverse();
            Console.WriteLine(i); // -20
            Console.WriteLine(i_rev); // 20
        }
    }

    public static class ExtensionMethods
    {
        public static Boolean Reverse(this Boolean value)
        {
            return !value;
        }

        public static Int32 Reverse(this Int32 value)
        {
            return -value;
        }
    }
}
public interface IInterface<T>
{
    T Reverse(T value);
}

public class Integers : IInterface<int>
{
    public int Reverse(int value)
    {
        return -value;
    }
}

public class Bools : IInterface<bool>
{
    public bool Reverse(bool value)
    {
        return !value;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Integers i = new Integers();
        Bools b = new Bools();

        Console.WriteLine(i.Reverse(5));
        Console.WriteLine(b.Reverse(true));
    }
}

选项编号2:

public class ReverseClass
{
    const string INT_TYPE = "System.Int32";
    const string BOOL_TYPE = "System.Boolean";

    public Object Reverse(Object value)
    {
        string type = value.GetType().ToString();

        switch(type)
        {
            case INT_TYPE:
                return -(Int32)value;
            case BOOL_TYPE:
                return !(bool)value;
        }
        return null;
    }
}

class Program
{
    static void Main(string[] args)
    {
        ReverseClass x = new ReverseClass();

        Console.WriteLine(x.Reverse(5));
        Console.WriteLine(x.Reverse(true));
    }
}

首先,我要说的是,您的方法违背了使用接口和泛型类型的目的。 您使用的是通用类型接口,但是对逆向机制进行了硬编码,它并没有真正帮助您,不妨在方法或扩展中对它们进行硬编码(建议使用@mjwills)。

无论如何,我仍然会为您解决问题,您的方法可能是一种学习的方式。

您的IInterface<T>很好,但是实现它的类却不好。 他们将必须指定使用的类型,如下所示:

public interface IInterface<T>
{
    T Reverse(T value);
}

public class Integers : IInterface<int>
{
    public int Reverse(int value)
    {
        return -value;
    }
}

public class Bools : IInterface<bool>
{
    public bool Reverse(bool value)
    {
        return !value;
    }
}

为了使用它,您必须创建每个类的实例并使用它们各自的Reverse方法,如下所示:

var integers = new Integers();
Console.WriteLine(integers.Reverse(1)); // -1

var bools = new Bools();
Console.WriteLine(bools.Reverse(true)); // False

好吧,我认为为此,您的解决方案有点不合适:

我不想在integers.Reverse()上调用它,而是使它更“通用”,例如: x.Reverse(1); x.Reverse(false); x.Reverse(1); x.Reverse(false); 那可能吗? x可以是任意值。。但是我想避免使用ExtensionMethods方法。

暂无
暂无

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

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