yes it's a bug in BCL..Use Attribute.GetCustomAttribute(type,attributeType) instead.
2010/8/10 omer katz <[email protected]>: > namespace DynamicProxyTest > { > internal class LookupPropertyInterceptor : IInterceptor > { > public void Intercept(IInvocation invocation) > { > string propertyName = invocation.Method.Name.Substring(4); > var attributes = > (LookupProperty[])invocation.Proxy.GetType().GetProperty(propertyName) > > .GetCustomAttributes(typeof(LookupProperty), true); > bool isLookup = attributes.OfType<LookupProperty>().Any(); > > if (!isLookup) > { > invocation.Proceed(); > return; > } > > if (invocation.Method.Name.StartsWith("set_", > StringComparison.OrdinalIgnoreCase) && > attributes[0] != null > ) > { > //var property = ((LookupProperty)attributes[0]); > var obj = invocation.Proxy; > Console.WriteLine(invocation.Arguments[0]); > > } > > if (invocation.Method.Name.StartsWith("get_", > StringComparison.OrdinalIgnoreCase)/* && > attributes.Count == 1*/ > ) > { > //var property = ((LookupProperty)attributes[0]); > var obj = invocation.Proxy; > > } > > invocation.Proceed(); > } > } > [Entity("Test")] > public class MyModel > { > private static readonly ProxyGenerator _generator = new > ProxyGenerator(); > > public static T MakeModel<T>() where T : class, new() > { > var proxy = _generator.CreateClassProxy<T>(new > LookupPropertyInterceptor()); > > return proxy; > } > > > [LookupProperty("SomeLookup", "SomeOtherEntity")] > public virtual int X { get; set; } > } > > class Program > { > static void Main(string[] args) > { > var proxy = MyModel.MakeModel<MyModel>(); > proxy.X = 5; > MyModel m = new MyModel(); > var xxx = (LookupProperty[])m.GetType().GetProperty("X") > > .GetCustomAttributes(typeof(LookupProperty), true);; > Console.ReadLine(); > } > } > } > > Turns out the proxy doesn't copy the attributes. > I had to refer to the TargetingType. > Is this a bug? > > 2010/8/10 Krzysztof Koźmic <[email protected]> >> >> It makes the source code far less readable and tends to not get maintained >> once written. >> >> On 10/08/2010 10:17 PM, omer katz wrote: >> >> Why do you think it's noise? >> >> 2010/8/10 Krzysztof Koźmic <[email protected]> >>> >>> none. >>> >>> XML comments in code are usually noise and Dynamic Proxy is actually one >>> of very few places in Castle project that does have them. >>> >>> >>> >>> On 10/08/2010 9:58 PM, omer katz wrote: >>> >>> It's much more comfortable to have them generated to webpages. >>> What auto-generation docs are you guys using? >>> >>> 2010/8/10 Krzysztof Koźmic <[email protected]> >>>> >>>> plus you get xml docs on the interface if it wasnt obvious enough >>>> /// <summary> >>>> /// Encapsulates an invocation of a proxied method. >>>> /// </summary> >>>> public interface IInvocation >>>> { >>>> /// <summary> >>>> /// Gets the proxy object on which the intercepted method is >>>> invoked. >>>> /// </summary> >>>> /// <value>Proxy object on which the intercepted method is >>>> invoked.</value> >>>> object Proxy { get; } >>>> ... >>>> >>>> On 10/08/2010 9:12 PM, omer katz wrote: >>>> >>>> Is it invocation.Proxy? >>>> It's pretty annoying that the API is not documented. >>>> >>>> 2010/8/10 Ken Egozi <[email protected]> >>>>> >>>>> you've got (at least) two options at your disposal: >>>>> a. the invocation object has a reference to the object it was running >>>>> on (I don't remember the name of the reference by heart). you can then >>>>> grab >>>>> the dictionary from there using reflection >>>>> a'. better yet, have the dictionary be an explicitly implemented >>>>> interface, and get typed access for it in the interceptor, without >>>>> polluting >>>>> your entity's API >>>>> b. you could also keep the dictionary on the interceptor instance (and >>>>> make sure you have interceptor per entity and not as singleton) which will >>>>> make accessing it a breeze. >>>>> example for something similar can be seen >>>>> here: http://daniel.wertheim.se/2010/02/05/getting-started-with-mongodb-using-json-net-and-castle-dynamic-proxy/ >>>>> >>>>> On Tue, Aug 10, 2010 at 12:20 PM, omer katz <[email protected]> wrote: >>>>>> >>>>>> So I've gotten this far: >>>>>>> >>>>>>> internal >>>>>>> >>>>>>> class LookupPropertyInterceptor : IInterceptor >>>>>>> >>>>>>> { >>>>>>> >>>>>>> public void Intercept(IInvocation invocation) >>>>>>> >>>>>>> { >>>>>>> >>>>>>> if (invocation.Method.Name.StartsWith("set_", >>>>>>> StringComparison.OrdinalIgnoreCase) && >>>>>>> >>>>>>> invocation.Method.GetCustomAttributes( >>>>>>> >>>>>>> typeof(LookupProperty), true).ToList().Count == 1) >>>>>>> >>>>>>> { >>>>>>> >>>>>>> var property = ((LookupProperty) >>>>>>> invocation.Method.GetCustomAttributes(typeof(LookupProperty), >>>>>>> true).ToList()[0]); // Handle property here >>>>>>> >>>>>>> } >>>>>>> >>>>>>> if (invocation.Method.Name.StartsWith("get_", >>>>>>> StringComparison.OrdinalIgnoreCase) && >>>>>>> >>>>>>> invocation.Method.GetCustomAttributes( >>>>>>> >>>>>>> typeof(LookupProperty), true).ToList().Count == 1) >>>>>>> >>>>>>> { >>>>>>> >>>>>>> var >>>>>> >>>>>> property = ((LookupProperty) >>>>>> invocation.Method.GetCustomAttributes(typeof(LookupProperty), >>>>>> true).ToList()[0]); // Handle property here >>>>>> >>>>>> } >>>>>> >>>>>> invocation.Proceed(); >>>>>> >>>>>> } >>>>>> >>>>>> } >>>>>> >>>>>> Now how do I handle the setting and getting to the dict dictionary >>>>>> from the source object? >>>>>> >>>>>> 2010/8/10 Ken Egozi <[email protected]> >>>>>>> >>>>>>> while intercepting you have access to the intercepted MethodInfo. >>>>>>> from there to the attributes set on that method the road is clear >>>>>>> >>>>>>> On Tue, Aug 10, 2010 at 11:26 AM, omer katz <[email protected]> wrote: >>>>>>>> >>>>>>>> Hello, >>>>>>>> I am reading Krzysztof Koźmic's tutorial about Dynamic Proxy and I >>>>>>>> can't figure out how to intercept all properties that have some custom >>>>>>>> attribute. >>>>>>>> >>>>>>>> Example: >>>>>>>>> >>>>>>>>> [ >>>>>>>>> >>>>>>>>> Entity("MyEntityName")] public class MyEntity >>>>>>>>> >>>>>>>>> { >>>>>>>>> >>>>>>>>> private Dictionary<string, object> dict = new >>>>>>>>> Dictionary<string, object>(); >>>>>>>>> >>>>>>>>> [ >>>>>>>>> >>>>>>>>> LookupProperty("SomeOtherEntityName")] public virtual >>>>>>>>> SomeOtherEntity SomeOtherEntity { get; set; } >>>>>>>>> >>>>>>>>> } >>>>>>>> >>>>>>>> In the end result I would like SomeOtherEntity to access the dict >>>>>>>> dictionary with "SomeOtherEntityName" as the key. >>>>>>>> >>>>>>>> Is it possible? How can it be done? >>>>>>>> >>>>>>>> -- >>>>>>>> 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. >>>>>>> >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Ken Egozi. >>>>>>> http://www.kenegozi.com/blog >>>>>>> http://www.delver.com >>>>>>> http://www.musicglue.com >>>>>>> http://www.castleproject.org >>>>>>> http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו >>>>>>> בהמוניכם >>>>>>> -- >>>>>>> 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. >>>>>> >>>>>> -- >>>>>> 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. >>>>> >>>>> >>>>> >>>>> -- >>>>> Ken Egozi. >>>>> http://www.kenegozi.com/blog >>>>> http://www.delver.com >>>>> http://www.musicglue.com >>>>> http://www.castleproject.org >>>>> http://www.idcc.co.il - הכנס הקהילתי הראשון למפתחי דוטנט - בואו >>>>> בהמוניכם >>>> >>>> -- >>>> 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. >>>> -- >>>> 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. >>>> >>>> -- >>>> 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. >>> >>> -- >>> 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. >>> >>> -- >>> 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. >> >> -- >> 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. >> >> -- >> 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. > > -- > 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. > -- 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.
