如何扩展匿名类的对象

发布于 2024-10-08 16:44:54 字数 381 浏览 6 评论 0原文

我有类方法:

public object MyMethod(object obj)
{
   // I want to add some new properties example "AddedProperty = true"
   // What must be here?
   // ...

   return extendedObject;
}

并且:

var extendedObject = this.MyMethod( new {
   FirstProperty = "abcd",
   SecondProperty = 100 
});

现在extendObject 有了新属性。请帮忙。

I have class method:

public object MyMethod(object obj)
{
   // I want to add some new properties example "AddedProperty = true"
   // What must be here?
   // ...

   return extendedObject;
}

And:

var extendedObject = this.MyMethod( new {
   FirstProperty = "abcd",
   SecondProperty = 100 
});

Now the extendedObject has new properties. Help please.

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

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

发布评论

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

评论(3

赴月观长安 2024-10-15 16:44:54

你不能那样做。

如果您想要一个可以在运行时添加成员的动态类型,那么您可以使用 ExpandoObject

表示一个对象,其成员可以在运行时动态添加和删除。

这需要 .NET 4.0 或更高版本。

You can't do that.

If you want a dynamic type that you can add members to at runtime then you could use an ExpandoObject.

Represents an object whose members can be dynamically added and removed at run time.

This requires .NET 4.0 or newer.

旧人九事 2024-10-15 16:44:54

您可以使用字典(属性、值),或者如果您使用的是 c# 4.0,则可以使用新的动态对象 (ExpandoObject)。

http://msdn.microsoft.com/en-us/library/dd264736.aspx

You can use a Dictionary (property, value) or if you are using c# 4.0 you can use the new dynamic object (ExpandoObject).

http://msdn.microsoft.com/en-us/library/dd264736.aspx

错爱 2024-10-15 16:44:54

您在编译时知道属性的名称吗?因为您可以这样做:

public static T CastByExample<T>(object o, T example) {
    return (T)o;
}

public static object MyMethod(object obj) {
    var example = new { FirstProperty = "abcd", SecondProperty = 100 };
    var casted = CastByExample(obj, example);

    return new {
        FirstProperty = casted.FirstProperty,
        SecondProperty = casted.SecondProperty,
        AddedProperty = true
    };
}

然后:

var extendedObject = MyMethod(
    new {
        FirstProperty = "abcd",
        SecondProperty = 100
    }
);

var casted = CastByExample(
    extendedObject,
    new {
        FirstProperty = "abcd",
        SecondProperty = 100,
        AddedProperty = true 
    }
);
Console.WriteLine(xyz.AddedProperty);

请注意,这在很大程度上依赖于以下事实:同一程序集中的两个匿名类型,其属性具有相同名称、相同类型、相同顺序,并且属于相同类型。

但是,如果您要这样做,为什么不直接创建具体类型呢?

输出:

True

Do you know at compile-time the names of the properties? Because you can do this:

public static T CastByExample<T>(object o, T example) {
    return (T)o;
}

public static object MyMethod(object obj) {
    var example = new { FirstProperty = "abcd", SecondProperty = 100 };
    var casted = CastByExample(obj, example);

    return new {
        FirstProperty = casted.FirstProperty,
        SecondProperty = casted.SecondProperty,
        AddedProperty = true
    };
}

Then:

var extendedObject = MyMethod(
    new {
        FirstProperty = "abcd",
        SecondProperty = 100
    }
);

var casted = CastByExample(
    extendedObject,
    new {
        FirstProperty = "abcd",
        SecondProperty = 100,
        AddedProperty = true 
    }
);
Console.WriteLine(xyz.AddedProperty);

Note that this relies very heavily on the fact that two anonymous types in the same assembly with properties having the same name of the same type in the same order are of the same type.

But, if you're going to do this, why not just make concrete types?

Output:

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