English 中文(简体)
现有物体的沥滤/动态轴
原标题:Duck typing / dynamic proxies on existing instances of objects

我把一个物体交给我们的图书馆,通过各种程序。 我需要对这些物体附加一些信息,因为这些物体经过了不同阶段,并且从另一个方面走过来——一种动态的腐蚀模式,我猜测,但增加额外财产而不是改变现有行为。

I was hoping to use LinFu or Castle to create a dynamic proxy and implement an additional interface on the object to store this. Components that know about the extended interface could cast and access it - whilst those that are not are oblivious, as the underlying type has not changed.

然而,我很不赞赏的是,所有这些机制都假定你控制了当初创建这种类型的地点——我没有这样做。

是否有人建议我如何更好地处理这一问题?

很多人

最佳回答

这完全没有涉及你的情景,而是如何使用一种动态的客观执行,作为你们的物体的cor子? 这将使你能够接触原始物体和额外财产。 缺乏类似扩大目标,但首先依靠自己的实例数据。

与此类似:

public class Expando : DynamicObject
{
    public dynamic Instance;
    Dictionary<string, dynamic> ExtraProperties = new Dictionary<string, dynamic>();

    public Expando(object instance)
    {
        Instance = instance;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        try
        {
            result = ReflectionUtils.GetProperty(Instance, binder.Name);
            return true;
        }
        catch
        {
            if (ExtraProperties.Keys.Contains(binder.Name))
            {
                result = ExtraProperties[binder.Name];
                return true;
            }
        }

        result = null;
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        try
        {
            ReflectionUtils.SetProperty(Instance, binder.Name, value);
        }
        catch (Exception ex)
        {
            ExtraProperties[binder.Name] = value;
        }

        return true;
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        try
        {
            result = ReflectionUtils.CallMethod(Instance, binder.Name, args);
            return true;
        }
        catch
        {}

        result = null;
        return false;
    }
}

但不幸的是,由于这里的反思使用(从

问题回答

象高技能这样的种子只是创造了一个新类别,只包含你们的“外来”特性。 固定<代码>Dictionary<MainClass,ExtensionsClass>。 当贵组成部分“知道”想要看一物体的延伸特性时,它们只是看着该词。

您希望采用以下办法:,在C#的操作时间将房地产扩大到一个类型物体。

这样一来,你就获得记忆泄漏。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签