【问题标题】:C# Anonymous Type declarationC# 匿名类型声明
【发布时间】:2011-03-10 10:17:04
【问题描述】:

我有一个静态类,其方法使用 linq 并返回一个对象。我的编译器不想编译它,因为他需要对象的定义。你能告诉我我有哪些意见来定义对象吗?

我正在寻找一个微小的解决方案,我不想为它创建一个额外的类(如果不需要的话?)

public static object GetWaveAnimation()
{
    return (from element in configurations.Elements("Animation")
            where element.Attribute("NAME").Value == "Wave"
            select new
                {
                    time = element.Attribute("TIMING").Value,
                    enable = element.Attribute("ENABLED").Value
                }).FirstOrDefault();
}

【问题讨论】:

  • 错误信息是什么?您知道您不能使用 var 让编译器为您推断返回类型,但返回 object 应该可以编译。
  • Noooooo......删除那个catch块。为什么你认为你需要它?
  • 哦,你是对的,一开始我没有“FirstOrDefault()”,我不确定如果找不到 XElement 函数是否会抛出异常

标签: c# anonymous-types var


【解决方案1】:

如果您想要一个静态类型(和命名)的解决方案,您应该创建一个单独的类。 有一些避免它的 hacky 方法,但总的来说这不是一个好主意。

如果您使用的是 .NET 4,另一种选择是返回 IEnumerable<Tuple<string, string>>。这样您会丢失“时间”和“启用”名称,但请记住它是一对字符串。

【讨论】:

  • 在 OP 的情况下,IEnumerable 可能不需要。一个简单的Tuple 就可以了。
  • 我正在使用 .NET 3.5 IENumerable 是我最初的想法之一,但我无法理解。你能给我发一个 IEnumerable 的例子吗?
【解决方案2】:

另一种解决方案:Hidden Features of C#?

// Useful? probably not.
private void foo()
{
    var user = AnonCast(GetUserTuple(), new { Name = default(string), Badges = default(int) });
    Console.WriteLine("Name: {0} Badges: {1}", user.Name, user.Badges);
}

object GetUserTuple()
{
    return new { Name = "dp", Badges = 5 };
}    

// Using the magic of Type Inference...
static T AnonCast<T>(object obj, T type)
{
   return (T) obj;
}

【讨论】:

    【解决方案3】:

    对于 .net 3.5,只要咬紧牙关,这是最干净的解决方案。

    public struct Wave{
         public X time;
         public Y enable;
    }
    
    public static Wave GetWaveAnimation()
        {
            try
            {
                return (from element in configurations.Elements("Animation")
                        where element.Attribute("NAME").Value == "Wave"
                        select new Wave
                            {
                                time = element.Attribute("TIMING").Value,
                                enable = element.Attribute("ENABLED").Value
                            }).FirstOrDefault();
            }
            catch { return null; }
        }
    

    对于 .net 4.0,您可以使用 dynamic 关键字(但您不能从您的程序集或朋友程序集外部调用此方法,因为匿名类型是内部的。)

     public static dynamic GetWaveAnimation()
    {
        try
        {
            return (from element in configurations.Elements("Animation")
                    where element.Attribute("NAME").Value == "Wave"
                    select new
                        {
                            time = element.Attribute("TIMING").Value,
                            enable = element.Attribute("ENABLED").Value
                        }).FirstOrDefault();
        }
        catch { return null; }
    }
    

    或者你有元组选项

      public static Tuple<X,Y> GetWaveAnimation()
            {
                try
                {
                    return (from element in configurations.Elements("Animation")
                            where element.Attribute("NAME").Value == "Wave"
                            select Tuple.Create(
                                       element.Attribute("TIMING").Value,
                                       element.Attribute("ENABLED").Value
                                    )
                                }).FirstOrDefault();
                }
                catch { return null; }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-16
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      相关资源
      最近更新 更多