如何将属性与在混合实例上定义的动态代理混合?

How can I mixin attributes with Dynamic Proxy defined on mixin instances?

我定义了以下 mixin:

public interface IMixin
{
  string SomeProperty { get; set; }
}

public class Mixin : IMixin
{
  [SomeAttribute]
  public string SomeProperty { get; set; }
}

这会注入以下 "proxy generating"-调用:

using Castle.DynamicProxy;

var proxyGenerationOptions = new ProxyGenerationOptions();
var mixin = new Mixin();
proxyGenerationsOptions.AddMixinInstance(mixin);
var additionalInterfacesToProxy = new []
                                  {
                                    typeof (IMixin)
                                  };
var proxyGenerator = new ProxyGenerator();
var proxy = proxyGenerator.CreateClassProxy(/* base type */,
                                            additionalInterfacesToProxy,
                                            proxyGenerationOptions,
                                            /* interceptor instance */);

我面临的问题:

var instance = /* proxy instance */;
var propertyInfo = instance.GetType()
                           .GetProperty(nameof(IMixin.SomeProperty));
var attribute = Attribute.GetCustomAttribute(propertyInfo,
                                             typeof(SomeAttribute),
                                             true);

attribute 为空。

如何混合具体实例,包括类型中定义的属性(properties/class/methods/fields/...)?

有一个可用的解决方法:

using System.Reflection.Emit;
using Castle.DynamicProxy;

var proxyGenerationOptions = new ProxyGenerationsOptions();
/* ... */
var customAttributeBuilder = new CustomAttributeBuilder(/* ... */);
proxyGenerationOptions.AdditionalAttributes.Add(customAttributeBuilder);

唯一的缺点:不能在类型的成员上定义任何属性,只能在 class 本身上定义。