0

Ich habe folgende mixin definiert:Wie kann ich Attribute mit Dynamic Proxy mischen, die auf Mixin-Instanzen definiert sind?

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

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

Das mit dem folgenden "Proxy-Erzeugungs" -Aufruf injiziert wird:

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 */); 

Das Problem, das ich bin vor:

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

attribute ist Null.

Wie kann ich eine konkrete Instanz mischen, einschließlich der Attribute, die im Typ definiert sind (über properties/class/methods/fields/...)?

+0

Was sagt 'instance.GetType(). FullName'? –

+0

@ LasseV.Karlsen 'Castle.Proxies. ** BaseType ** Proxy',' propertyInfo' ist nicht null, daher wird das Mixin entsprechend den 'proxyGenerationOptions' angewendet. –

+0

Ich vermute, Sie wollen nicht das Attribut auf "IMixin" -Schnittstelle? –

Antwort

0

Es gibt eine provisorische Lösung des Problems:

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

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

Der einzige Nachteil: Sie können keine Attribute auf Mitglieder des Typs definieren, nur auf die Klasse selbst.

Verwandte Themen