简体   繁体   English

如何访问返回通用类型的类中的方法

[英]How can I access to the method in class that returns a Generic Type

I have a class named config with two string fields named key paramValue and parameterPath. 我有一个名为config的类,其中有两个名为key paramValue和parameterPath的字符串字段。

When I apply the ChooseType method of the class, the method has to return one variable paramValue in different types (Int or bool or String). 当我应用该类的ChooseType方法时,该方法必须返回一种不同类型(Int或bool或String)的变量paramValue。

I implemented it as follow: 我实现它如下:

  class ConfigValue
  {
      public string paramPath;
      private string paramValue;

      public enum RetType {RetInt, RetBool, RetString};

       public T PolimorphProperty<T>(RetType how) 
       {

          { 
            switch (how)
             {
             case RetType.RetInt:
               return (dynamic)int.Parse(paramValue);

             case RetType.RetBool:
               return (dynamic)Boolean.Parse(paramValue);

             case RetType.RetString:
               return (T)(object)paramValue;

             default:
               throw new ArgumentException("RetType not supported", "how");

              }
          }   
      }
  }

My question is how can i access to the PolimorphProperty method in ConfigValue class, to retrive for examlple paramValue Int type. 我的问题是如何访问ConfigValue类中的PolimorphProperty方法,以检索示例paramValue Int类型。

Having both T and RetType is redundant. 同时拥有TRetType是多余的。 It should be something like this: 应该是这样的:

class ConfigValue
{
    public string paramPath;
    private string paramValue;

    public T PolimorphProperty<T>()
    {
        return (T)Convert.ChangeType(paramValue, typeof(T));
    }
}

Call it like configValue.PolimorphProperty<int>() . configValue.PolimorphProperty<int>()这样称呼它。

Or if you need to implement the type conversion manually, you can do something like this: 或者,如果您需要手动实现类型转换,则可以执行以下操作:

class ConfigValue
{
    public string paramPath;
    private string paramValue;

    public T PolimorphProperty<T>()
    {
        if (typeof(T) == typeof(MySpecialType))
            return (T)(object)new MySpecialType(paramValue);
        else
            return (T)Convert.ChangeType(paramValue, typeof(T));
    }
}

I think following code best matches what you want (i have tested it before writing here...) 我认为以下代码最符合您的需求(我在这里编写之前已经对其进行了测试...)

public T PolimorphProperty<T>()
{
      object tt = Convert.ChangeType(paramValue, typeof(T));
      if (tt == null)
         return default(T);
      return (T) tt;
}

And you can call the code like this: 您可以这样调用代码:

 int ret = cv.PolimorphProperty<int>();

Notes: 笔记:

  • You really do not need to pass anything in the param list to determine the type of the returned value. 您实际上不需要传递任何参数列表中的值即可确定返回值的类型。
  • Make sure you put try-catch wherever you are checking the appropraite type for your future usage. 确保在检查适当类型以备将来使用的任何地方放置try-catch。

暂无
暂无

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

相关问题 如何在泛型 class 中访问 T 类型的 static 属性? - How can I access a static property of type T in a generic class? 我可以在泛型类型 class 中有不同的泛型类型方法吗 - Can I have a different generic type Method in a generic type class 如何基于 class 上的泛型类型在泛型方法上约束类型 - How can I constrain a type on a generic method based on a generic type on the class 无法访问我在泛型方法中用作类型参数的 class 的基数 class 的属性 - Can't access properties of the base class of the class I am using as the type argument in a generic method 如何在不通知父类该泛型类型的情况下在注入的类上调用泛型方法? - How can I call a generic method on an injected class without informing the parent class of the generic type? 如何定义一个泛型方法来返回派生类类型的实例? - How to define a generic method that returns an instance of the deriving class type? 我如何获得一个采用通用类型的类,以从该类型的实例中调用方法? - How can I get a class that takes generic type, to call a method from an instance of that type? 通用方法类型不能用作通用类的通用类型 - Generic method type can't be used as generic type for generic class 通用类型约束——我可以在方法级别上扩展类约束吗? - Generic type constraints - can I extend class constraint on method level? 当T是具有未知参数的泛型类时,如何访问类型T的对象上的属性? - How can I access property on object of type T, when T is a generic class with unknown parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM