【问题标题】:multi return type in c# methodsc#方法中的多返回类型
【发布时间】:2013-04-07 15:07:12
【问题描述】:

我有一个(字符串,对象)字典,对象(类)有一些值,包括由枚举定义的数据类型。我需要一个应该返回字典项值的 GetItemValue 方法。所以返回类型必须是项目对象中定义的类型。

Class Item
{
    String Name;
    DataValueType DataType;
    Object DataValue;
}

private Dictionary<string, Item> ItemList = new Dictionary<string, Item>();

void Main()
{
    int value;

    ItemList.Add("IntItem", new Item("IntItem", DataValueType.TInt, 123));
    value = GetItemValue("IntItem"); // value = 123
}

什么样的解决方案可以克服这个问题?

最好的问候,

【问题讨论】:

    标签: c# overloading return-type


    【解决方案1】:

    您可以使用Generic Classes

    Class Item<T>
    {
        String Name;
        T DataTypeObject;
        Object DataValue;
    
        public T GetItemValue()
        { 
            //Your code
            return DataTypeObject;
        }
    }
    

    【讨论】:

      【解决方案2】:

      更好的解决方案是引入一个让所有类都实现的接口。请注意,接口不一定要指定任何行为:

      public interface ICanBePutInTheSpecialDictionary {
      }
      
      public class ItemTypeA : ICanBePutInTheSpecialDictionary {
          // code for the first type
      }
      
      public class ItemTypeB : ICanBePutInTheSpecialDictionary {
          // code for the second type
      }
      // etc for all the types you want to put in the dictionary
      

      将东西放入字典:

      var dict = new Dictionary<string, ICanBePutInTheSpecialDictionary>();
      
      dict.add("typeA", new ItemTypeA());
      dict.add("typeB", new ItemTypeB());
      

      当您需要将对象转换为特定类型时,您可以使用if-elseif-block,类似

      var obj = dict["typeA"];
      if (obj is ItemTypeA) {
          var a = obj as ItemTypeA;
          // Do stuff with an ItemTypeA. 
          // You probably want to call a separate method for this.
      } elseif (obj is ItemTypeB) {
          // do stuff with an ItemTypeB
      }
      

      或使用反射。根据您有多少选择,任何一个都可能是可取的。

      【讨论】:

        【解决方案3】:

        如果你有一个“混合包”,你可以做这样的事情......

        class Item<T>
        {
            public String Name { get; set; }
            public DataValueType DataType { get; set; }
            public T DataValue { get; set; }
        }
        class ItemRepository
        {
            private Dictionary<string, object> ItemList = new Dictionary<string, object>();
        
            public void Add<T>(Item<T> item) { ItemList[item.Name] = item; }
            public T GetItemValue<T>(string key)
            {
                var item = ItemList[key] as Item<T>;
                return item != null ? item.DataValue : default(T);
            }
        }
        

        并像...一样使用它

        var repository = new ItemRepository();
        int value;
        repository.Add(new Item<int> { Name = "IntItem", DataType = DataValueType.TInt, DataValue = 123 });
        value = repository.GetItemValue<int>("IntItem");
        

        如果您只有几种类型 - 最好使用 Repository&lt;T&gt;

        【讨论】:

          【解决方案4】:

          我找到了我想要的解决方案。感谢谷歌叔叔。 感谢大家的关心。

           public dynamic GetValue(string name)
           {
               if (OpcDataList[name].IsChanged)
               {
                   OpcReflectItem tmpItem = OpcDataList[name];
                   tmpItem.IsChanged = false;
                   OpcDataList[name] = tmpItem;
               }
          
               return Convert.ChangeType(OpcDataList[name].ItemValue.Value, OpcDataList[name].DataType);
           }
          

          【讨论】:

          • 你应该只使用object
          猜你喜欢
          • 2022-12-17
          • 1970-01-01
          • 2018-03-16
          • 2019-04-05
          • 2012-05-17
          • 2018-12-08
          • 2014-08-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多