C# 匿名类型声明

发布于 2024-10-21 12:32:54 字数 518 浏览 1 评论 0 原文

我有一个静态类,其方法使用 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尸血腥色 2024-10-28 12:32:54

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

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

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.

街道布景 2024-10-28 12:32:54

另一个解决方案: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;
}

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;
}
忘你却要生生世世 2024-10-28 12:32:54

对于 .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; }
}

OR 您有元组选项

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

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; }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文