创建像 ASP.NET MVC 3 ViewBag 这样的类?

发布于 2024-11-03 15:54:01 字数 108 浏览 0 评论 0原文

我有一种情况,我想做一些类似于 ASP.NET MVC 3 ViewBag 对象所做的事情,其中​​属性是在运行时创建的?或者是在编译时?

无论如何,我想知道如何创建具有这种行为的对象?

I have a situation where I would like to do something simular to what was done with the ASP.NET MVC 3 ViewBag object where properties are created at runtime? Or is it at compile time?

Anyway I was wondering how to go about creating an object with this behaviour?

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

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

发布评论

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

评论(5

悲凉≈ 2024-11-10 15:54:01

我创建了这样的东西:

public class MyBag : DynamicObject
{
    private readonly Dictionary<string, dynamic> _properties = new Dictionary<string, dynamic>( StringComparer.InvariantCultureIgnoreCase );

    public override bool TryGetMember( GetMemberBinder binder, out dynamic result )
    {
        result = this._properties.ContainsKey( binder.Name ) ? this._properties[ binder.Name ] : null;

        return true;
    }

    public override bool TrySetMember( SetMemberBinder binder, dynamic value )
    {
        if( value == null )
        {
            if( _properties.ContainsKey( binder.Name ) )
                _properties.Remove( binder.Name );
        }
        else
            _properties[ binder.Name ] = value;

        return true;
    }
}

然后你可以像这样使用它:

dynamic bag = new MyBag();

bag.Apples = 4;
bag.ApplesBrand = "some brand";

MessageBox.Show( string.Format( "Apples: {0}, Brand: {1}, Non-Existing-Key: {2}", bag.Apples, bag.ApplesBrand, bag.JAJA ) );

注意“JAJA”的条目从未创建......并且仍然不会抛出异常,只是返回 null

希望这对某人有帮助

I created something like this:

public class MyBag : DynamicObject
{
    private readonly Dictionary<string, dynamic> _properties = new Dictionary<string, dynamic>( StringComparer.InvariantCultureIgnoreCase );

    public override bool TryGetMember( GetMemberBinder binder, out dynamic result )
    {
        result = this._properties.ContainsKey( binder.Name ) ? this._properties[ binder.Name ] : null;

        return true;
    }

    public override bool TrySetMember( SetMemberBinder binder, dynamic value )
    {
        if( value == null )
        {
            if( _properties.ContainsKey( binder.Name ) )
                _properties.Remove( binder.Name );
        }
        else
            _properties[ binder.Name ] = value;

        return true;
    }
}

then you can use it like this:

dynamic bag = new MyBag();

bag.Apples = 4;
bag.ApplesBrand = "some brand";

MessageBox.Show( string.Format( "Apples: {0}, Brand: {1}, Non-Existing-Key: {2}", bag.Apples, bag.ApplesBrand, bag.JAJA ) );

note that entry for "JAJA" was never created ... and still doesn't throw an exception, just returns null

hope this helps somebody

狂之美人 2024-11-10 15:54:01

从行为角度来看,ViewBag 的行为非常类似于 ExpandoObject,因此也许你想用什么。但是,如果您想执行自定义行为,您可以子类化 DynamicObject。在使用这些类型的对象时,dynamic 关键字很重要,因为它告诉编译器在运行时而不是编译时绑定方法调用,但是普通旧 clr 类型上的动态关键字只会避免类型检查,并且不会为您的对象提供 ExpandoObject 或 DynamicObject 的动态实现类型功能。

Behavior wise the ViewBag acts pretty much like ExpandoObject so that maybe what you want to use. However if you want to do custom behaviors you can subclass DynamicObject. The dynamic keyword is important when using these kinds of objects in that it tells to compiler to bind the method calls at runtime rather than compile time, however the dynamic keyword on a plain old clr type will just avoid type checking and won't give your object dynamic implementation type features that is what ExpandoObject or DynamicObject are for.

似梦非梦 2024-11-10 15:54:01

使用动态类型的对象。 请参阅本文了解详细信息。

Use an object of type dynamic. See this article for more information.

臻嫒无言 2024-11-10 15:54:01

ViewBag 声明如下:

dynamic ViewBag = new System.Dynamic.ExpandoObject();

ViewBag is declared like this:

dynamic ViewBag = new System.Dynamic.ExpandoObject();
一江春梦 2024-11-10 15:54:01

我想你想要一个匿名类型。请参阅 http://msdn.microsoft.com/en-us/library/bb397696。 aspx

例如:

var me = new { Name = "Richard", Occupation = "White hacker" };

然后你就可以像普通 C# 一样获取属性

Console.WriteLine(me.Name + " is a " + me.Occupation);

I think you want an anonymous type. See http://msdn.microsoft.com/en-us/library/bb397696.aspx

For example:

var me = new { Name = "Richard", Occupation = "White hacker" };

Then you can just get properties as in normal C#

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