2

私は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();
}
4

3 に答える 3

1

別の解決策: 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;
}
于 2011-03-23T23:38:52.713 に答える
1

静的に型付けされた (および名前が付けられた) ソリューションが必要な場合は、別のクラスを作成する必要があります。それを回避するハックな方法がいくつかありますが、一般的には良い考えではありません。

もう 1 つのオプションは、.NET 4 を使用している場合に戻るIEnumerable<Tuple<string, string>>ことです。この方法では、"time" と "enabled" の名前は失われますが、文字列のペアであるという考えは保持されます。

于 2011-03-10T10:20:10.090 に答える
0

.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では、動的キーワードを使用できます(ただし、匿名型は内部であるため、アセンブリまたはフレンドアセンブリの外部からこのメソッドを呼び出すことはできません)。

 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; }
        }
于 2011-03-18T17:21:29.993 に答える