开发者

C# Anonymous Type declaration

开发者 https://www.devze.com 2023-02-15 19:38 出处:网络
i have a static class with a method that use linq and returns an object. my compiler don´t want to compile it because he needs a definition for the object. can you tell me which opinions i have to 开

i have a static class with a method that use linq and returns an object. my compiler don´t want to compile it because he needs a definition for the object. can you tell me which opinions i have to 开发者_JAVA百科define the object?

i search for a tiny solution, i don´t want to create a extra class for it (if there is no need ?)

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


If you want a statically typed (and named) solution, you should create a separate class. There are some hacky ways of avoiding it, but it's not a good idea in general.

Another option is to return IEnumerable<Tuple<string, string>> if you're using .NET 4. That way you lose the "time" and "enabled" names, but keep the idea that it's a pair of strings.


another solution: 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;
}


For .net 3.5 just bite the bullet it's the cleanest looking solution.

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; }
    }

For .net 4.0 you can use dynamic keyword (but you can't call this method from outside your assembly or friend assemblies because anonymous types are internal.)

 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; }
}

OR you have the Tuple Option

  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; }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消