[quote] However, if you're needing this for WPF databinding, there might be a solution, although it's not yet implemented, and will be part of Dynamic Proxy 3. Take a look at Type Wrapping section: http://using.castleproject.org/display/CASTLE/Dynamic+Proxy+3+design+... [/quote]
Hum not sure it would well fit my need because the type of the bound entities is important : I need to get back some properties values that represents my real entities. For example the "IsSelected" property is defined by the "ISelectableEntity" interface that contains an "object", the underlying entity, that I need to get back when the state of the selection (determined by checkboxes or radio-buttons) changes. [quote] Currently you would have to manually disassemble your first mixin before mixing it again. Something like this: http://gist.github.com/214125 [/quote] It's just perfect Krzysztof, thanks a lot. I've wrapped this into an extension method : [code] public static class DynamicProxyExtensions { public static object CreateClassProxy(this ProxyGenerator thisProxyGenerator, object entity, ProxyGenerationOptions options) { Type type = entity.GetType(); IProxyTargetAccessor proxy = entity as IProxyTargetAccessor; if (proxy != null) { object target = proxy.DynProxyGetTarget(); type = target == proxy ? target.GetType().BaseType : target.GetType (); foreach (FieldInfo field in proxy.GetType().GetFields()) { if (field.Name.StartsWith("__mixin")) { options.AddMixinInstance(field.GetValue(proxy)); } } } return thisProxyGenerator.CreateClassProxy(type, null, options); } } [/code] Then, instead of doing : [code] ISelectableEntity selectableEntity = _proxyGenerator.CreateClassProxy (entity.GetType(), null, options) as ISelectableEntity; [/code] I do : [code] ISelectableEntity selectableEntity = _proxyGenerator.CreateClassProxy (entity, options) as ISelectableEntity; [/code] Thanks again for your precious help, it has allowed my application to enhance its design by getting rid of two wrapper classes and being more consistant across all the code. On 20 oct, 11:12, Krzysztof Koźmic (2) <[email protected]> wrote: > This is limitation of C# being a strongly typed language and the way > DynamicProxy works internally to try to overcome that limitations. > However, if you're needing this for WPF databinding, there might be a > solution, although it's not yet implemented, and will be part of > Dynamic Proxy 3. > Take a look at Type Wrapping > section:http://using.castleproject.org/display/CASTLE/Dynamic+Proxy+3+design+... > > Currently you would have to manually disassemble your first mixin > before mixing it again. > Something like this:http://gist.github.com/214125 > > HTH, > Krzysztof > > On 20 Paź, 10:52, serious <[email protected]> wrote: > > > Thanks for your quick answer Krzysztof. > > > [quote]Can't you just mix in all elements at once?[/quote] > > > If you mean : > > [code] > > ProxyGenerationOptions optionsAB = new ProxyGenerationOptions(); > > optionsAB.AddMixinInstance(new ADefaultImplementation()); > > optionsAB.AddMixinInstance(new BDefaultImplementation()); > > IB ab = _proxyGenerator.CreateClassProxy(typeof(C), null, optionsAB) > > as IB; > > [/code] > > no I can't. > > > [quote]What is your specific scenario here?[/quote] > > > Somewhere in my application I'm using DynamicProxy's mixins in a WPF > > custom DataGrid that dynamically add a "IsSelected" property to the > > entities : [Entity] -> [Entity+IsSelected] > > It works like a charm. > > > Elsewhere in my code, for a different purpose, I enrich some entities > > with a new property, "Name", by using mixins too : > > [Entity] -> [Entity+Name]. > > It also works perfectly. > > > Now I need to pass these enrich entities, which are proxies then, to > > the custom datagrid, which will try to generate a proxy for the > > entities, causing the issue. > > > Is it a definitive limitation, due to the .Net framework, or could it > > be overcome ? > > > On 20 oct, 10:23, Krzysztof Koźmic (2) <[email protected]> wrote: > > > > it's not really a bug - it's an unsupported scenario. > > > new DynamicProxy (version 2.2 from development branch) detects it and > > > throws meaningful exception ("Are you trying to proxy an existing > > > proxy?"). > > > Can't you just mix in all elements at once? What is your specific > > > scenario here? > > > > On 20 Paź, 10:16, serious <[email protected]> wrote: > > > > > Hello, > > > > > say I have an object and I want to obtain a new one whose class is a > > > > mixin of the object's class and some simple implementations of two > > > > interfaces. > > > > > Here is what I do : > > > > [code] > > > > using System; > > > > > using System.Reflection; > > > > > using Castle.DynamicProxy; > > > > > namespace Test > > > > { > > > > public interface IA > > > > { > > > > string SA > > > > { > > > > get; > > > > set; > > > > } > > > > } > > > > > public interface IB > > > > { > > > > string SB > > > > { > > > > get; > > > > set; > > > > } > > > > } > > > > > public class ADefaultImplementation : IA > > > > { > > > > public string SA > > > > { > > > > get; > > > > set; > > > > } > > > > } > > > > > public class BDefaultImplementation : IB > > > > { > > > > public string SB > > > > { > > > > get; > > > > set; > > > > } > > > > } > > > > > public class C > > > > { > > > > } > > > > > public class Application > > > > { > > > > private static void Main() > > > > { > > > > ProxyGenerator _proxyGenerator = new ProxyGenerator(); > > > > > // Creation of the first mixin instance "mixin1 = C + > > > > ADefaultImplementation" > > > > ProxyGenerationOptions optionsA = new > > > > ProxyGenerationOptions(); > > > > optionsA.AddMixinInstance(new ADefaultImplementation()); > > > > IA a = _proxyGenerator.CreateClassProxy(typeof(C), null, > > > > optionsA) as IA; > > > > > // Creation of the second mixin instance "mixin2 = mixin1 > > > > + BDefaultImplementation" > > > > ProxyGenerationOptions optionsB = new > > > > ProxyGenerationOptions(); > > > > optionsB.AddMixinInstance(new BDefaultImplementation()); > > > > IB b = _proxyGenerator.CreateClassProxy(a.GetType(), null, > > > > optionsB) as IB; > > > > > Console.WriteLine(b.SB); // OK > > > > Console.WriteLine((b as IA).SA); // KO > > > > } > > > > }} > > > > > [/code] > > > > But trying to access "SA" raises the following exception : > > > > [code] > > > > System.NullReferenceException was unhandled > > > > Message="Object reference not set to an instance of an object." > > > > Source="DynamicProxyGenAssembly2" > > > > StackTrace: > > > > at > > > > CProxy43c7f990b4df48bfaa238358baeb0bcf.Invocationget_SA_1.InvokeMethodOnTarget > > > > () > > > > at Castle.DynamicProxy.AbstractInvocation.Proceed() > > > > at Castle.Core.Interceptor.StandardInterceptor.Intercept > > > > (IInvocation invocation) > > > > at Castle.DynamicProxy.AbstractInvocation.Proceed() > > > > at CProxy43c7f990b4df48bfaa238358baeb0bcf.get_SA() > > > > at > > > > CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.get_SA_callback_4 > > > > () > > > > at > > > > CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.Invocationget_SA_4.InvokeMethodOnTarget > > > > () > > > > at Castle.DynamicProxy.AbstractInvocation.Proceed() > > > > at > > > > CProxy43c7f990b4df48bfaa238358baeb0bcfProxy3475609451fe4a69beb6ace21633e3e2.get_SA > > > > () > > > > at Test.Application.Main() in H:\p\test\wpf\TestForum\TestForum > > > > \Program.cs:line 67 > > > > at System.AppDomain._nExecuteAssembly(Assembly assembly, String > > > > [] args) > > > > at System.AppDomain.ExecuteAssembly(String assemblyFile, > > > > Evidence assemblySecurity, String[] args) > > > > at > > > > Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() > > > > at System.Threading.ThreadHelper.ThreadStart_Context(Object > > > > state) > > > > at System.Threading.ExecutionContext.Run(ExecutionContext > > > > executionContext, ContextCallback callback, Object state) > > > > at System.Threading.ThreadHelper.ThreadStart() > > > > InnerException: > > > > [/code] > > > > > Unfortunately notice that I can't use some basic working workarounds > > > > like : > > > > - using a unique mixin base "ABDefaultImplementation" with the two > > > > fields "SA" and "SB" this way : > > > > [code] > > > > public class ABDefaultImplementation : IA, IB > > > > { > > > > public string SA > > > > { > > > > get; > > > > set; > > > > } > > > > > public string SB > > > > { > > > > get; > > > > set; > > > > }} > > > > > ... > > > > ProxyGenerationOptions optionsAB = new ProxyGenerationOptions(); > > > > optionsAB.AddMixinInstance(new ABDefaultImplementation()); > > > > IB ab = _proxyGenerator.CreateClassProxy(typeof(C), null, optionsAB) > > > > as IB; > > > > [/code] > > > > - generating the final mixin object with a unique call to > > > > "CreateClassProxy" : > > > > [code] > > > > ProxyGenerationOptions optionsAB = new ProxyGenerationOptions(); > > > > optionsAB.AddMixinInstance(new ADefaultImplementation()); > > > > optionsAB.AddMixinInstance(new BDefaultImplementation()); > > > > IB ab = _proxyGenerator.CreateClassProxy(typeof(C), null, optionsAB) > > > > as IB; > > > > [/code] > > > > > Am I doing something wrong, is it a bug, or a known limitation ? > > > > The used version of "DynamicProxy" is 2.1. > > > > > Thanks by advance for your help. > > > > > Best regards. > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en -~----------~----~----~----~------~----~------~--~---
