开发者

Making "this" implicitly dynamic

I have a class that inherits from DynamicObject and overrides some of its members. I was wondering if there is any tricks that I could use that made 'this' implicitly dynamic (or at least the appearance of), so that when I want to dynamically add members inside the class I wouldn't have to do

((dynamic)this).Whatever = 10;

It would be much nicer if I could do

Whatever = 10;

I am just spiking some stuff so I can't add any real context to this and no, Ruby isn't the solution this time ;)开发者_运维技巧

I could wrap it in a property but it would add to the API of the functionality since I would have to do

Property.Whatever = 10;


Why not just extend the baseclass ?

public class DynamicObjectEx : DynamicObject
{
    protected dynamic self
    {
        get
        {
            return this;
        }
    }
}

Or you could simply switch to VB.NET with option strict off or whatever the setting is ;-)


It's not possible. Your approach with the Property.Whatever is the way to go.


Wouldn't it be easier to just add them as non-dynamic attributes? If you're using "this" within your class, you're sure that those attributes will already have to be there, no?

I don't know much about dynamic classes though so correct me if I'm wrong!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜