【问题标题】:Collection of different types C#不同类型 C# 的集合
【发布时间】:2021-06-13 11:23:40
【问题描述】:

我有两个类 OnlineBoolTag 和 OnlineDoubleTag。我将这些对象添加到列表中,并希望获取不同类型的值。如何返回 double 或 bool 的 Value 属性?

public class OnlineDoubleTag : IOnlineTag
{
    public string Name { get; set; }
    public double Value { get; set; }
}

public class OnlineBoolTag : IOnlineTag
{
    public string Name { get; set; }
    public bool Value { get; set; }
}

将对象添加到列表中:

var onlinetags = new List<IOnlineTag>();
onlinetags.Add(new OnlineBoolTag { Name = "Bool1", Value = true });
onlinetags.Add(new OnlineDoubleTag { Name = "Float1", Value = 777.22 });

foreach (var tag in onlinetags)
{
    Console.WriteLine(tag.*****Value*****);
}

【问题讨论】:

  • IOnlineTag 是如何定义的?
  • interface IOnlineTag { public object GetValue() }
  • 为什么不将 GetValue 方法实现为 return this.Value 并直接调用它?在您的实际需求中,您需要对 Value 做什么?
  • 如果我将接口实现为“返回 this.Value”,我将返回对象类型。但我需要 bool 或 double。

标签: c# list collections


【解决方案1】:

您可以使用dynamic 代替object

interface IOnlineTag
{
    public dynamic GetValue();
}

public class OnlineDoubleTag : IOnlineTag
{
    public string Name { get; set; }
    public double Value { get; set; }
    
    public dynamic GetValue()
    {
        return this.Value;
    }
}

public class OnlineBoolTag : IOnlineTag
{
    public string Name { get; set; }
    public bool Value { get; set; }
    
    public dynamic GetValue()
    {
        return this.Value;
    }
}

public static void Main()
{
    var onlinetags = new List<IOnlineTag>();
    onlinetags.Add(new OnlineBoolTag { Name = "Bool1", Value = true });
    onlinetags.Add(new OnlineDoubleTag { Name = "Float1", Value = 7777.22 });

    foreach (var tag in onlinetags)
    {
        Console.WriteLine($"{tag.GetValue()} {tag.GetValue().GetType()}");
    }
    
    // Value: True Type: System.Boolean
    // Value: 7777.22 Type: System.Double
}

【讨论】:

    【解决方案2】:

    正如盖伊的回答所指出的那样,在这里使用 dynamic 会有所帮助。不过,我们可以通过将属性 Value 本身定义为 dynamic 类型来进一步简化这一点。这消除了为 GetValue 方法实现接口的需要。相反,我们可以这样做:

    public class OnlineTag 
      {
        public string Name {get;set;}
        public dynamic Value {get;set;}
      }
    
    public class Program 
      {
        public static void Main(string[] args) 
        {
         var onlinetags = new List<OnlineTag>();
         onlinetags.Add(new OnlineTag { Name = "Bool1", Value = true });
         onlinetags.Add(new OnlineTag { Name = "Float1", Value = 7777.22 });
    
         foreach (var tag in onlinetags)
          {
            Console.WriteLine($"{tag.Value} {tag.Value.GetType()}");
            //prints the below
            //True System.Boolean
            //7777,22 System.Double
          }
        }
      }
    

    这样做的缺陷是,将 Value 定义为动态允许您稍后在代码中重新分配完全不同类型的值。例如,下面的代码不会抛出任何错误:

    Console.WriteLine($"{onlinetags[0].Value} {onlinetags[0].Value is bool}"); //prints True True
    onlinetags[0].Value = 123M;
    Console.WriteLine($"{onlinetags[0].Value} {onlinetags[0].Value.GetType()}"); // prints 123 System.Decimal
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2013-11-04
      • 1970-01-01
      相关资源
      最近更新 更多