简体   繁体   English

如何使用泛型类而不必声明泛型类型

[英]how to use a class with generics without having to declare the generic type

First of all, I am coming from Java background so please forgive me for my mistakes: 首先,我来自Java背景所以请原谅我的错误:

This is my class 这是我的班级

 class ParameterHandler<T>
 {

        public static Dictionary<string, Converter<T>> parameters = 
               new Dictionary<string, Converter<T>>();

        public static void addParameter(string parameterName, Converter<T> converter){
            parameters.Add(parameterName, converter);
        }

        static T getValue(string parameterName, string stringValue){
            return parameters[parameterName].getValue(stringValue);
        }
 }

and this is my Converter interface 这是我的Converter界面

interface Converter <T>
{
        T getValue(string stringValue);
}

I wonder if I can use the parameterhandler without having to declare T as a generic type for it 我想知道我是否可以使用parameterhandler而不必将T声明为它的泛型类型

I just need to do something like 我只需要做点什么

ParameterHandler.addParameter("some", Some Implementation for Converter)

and to put you in the whole picture, this is one of my implementation for the Converter 并且为了全面了解,这是我对Converter实现之一

class IntConverter : Converter<int>
{
        int getValue(string stringValue)
        {
            return int.Parse(stringValue);
        }
}

You can create a non-generic ParameterHandler class with a generic addParameter method: 您可以使用通用addParameter方法创建非泛型ParameterHandler类:

public static class ParameterHandler
{
    public static void addParameter<T>(string name, Converter<T> c)
    {
        ParameterHandler<T>.addParameter(name, c);
    }
}

then you can use 然后你可以使用

ParameterHandler.addParameter("some", new IntConverter());

You must define an interface that is base for all converters: 您必须定义一个基于所有转换器的接口:

 interface Converter
 {
 }
 interface Converter<T> : Converter
 {
     T getValue(string stringValue);
 }

Then: 然后:

class ParameterHandler
{
    public static Dictionary<string, Converter> parameters = 
           new Dictionary<string, Converter>();

    public static void addParameter<T>(string parameterName, Converter<T> converter){
        parameters.Add(parameterName, converter);
    }

    static T getValue<T>(string parameterName, string stringValue){
        if(!parameters[parameterName] is Convereter<T>)
             throw new Exception("Invalid Type");

        return ((Convereter<T>)parameters[parameterName]).getValue(stringValue);
    }
}

Note that, in your initial model, you have a different dictionary for each type T , Converter<int>.parameters and Converter<double>.parameters are not the same. 请注意,在初始模型中,每个类型T都有不同的字典, Converter<int>.parametersConverter<double>.parameters不相同。

暂无
暂无

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

相关问题 C#-如何在“通用类”类型的MainForm类中声明变量而不指定通用类型 - C# - How can I declare a variable in MainForm class of type “Generic Class” without specifing generic type 如何在没有第二个声明类型的情况下将辅助泛型参数传递给泛型类构造函数 - How to pass a secondary generic parameter to a generic class constructor without a second declare type 如何在 class 声明之外声明泛型类型 - How to declare a generic type outside of class declaration 如何声明可以接受可为空 Guid 的泛型类型类? - How to declare a generic type class that can accept nullable Guid? 如何声明具有数据成员字典类型的匿名类? - How to declare anonymous class having data member dictionary type? 在没有泛型的情况下,如何在基类上获取派生类的类型? - How to get the type of derived class at base class without generics? C#泛型:如何声明一个泛型,其中 2 个集合必须包含相同的类型,但集合不必是相同的类型 - C# Generics: How can I declare a generic where 2 collections have to contain the same type but the collections needn't be of the same type 如何声明泛型类型的泛型约束 - How to declare a generic constraint that is a generic type 使用 Type 类 (C#) 声明泛型 - declare a generic using Type class (C#) 我如何声明具有不同和未知类型的泛型列表?如何使用仅在运行时已知的类型初始化泛型? - How could I declare list of generics with different and unknown types and How could I initialize a generic with a type only known at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM