well i am not loosing all the attributes. only property attributes are
lost and i guess i figured out the reason for it. MethodInfo class
provides information on methods of a class in case of property there
are there two methods get_ and set_ and there are no attributes on
get_ or set_. The attribute is at property level. Therefore need to
get property on the type and get custom attribute on the property.
This is how I achieved it
public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
{
NotifyChangeAttribute notifyChangeAttribute;
if (methodInfo.Name.StartsWith("set_"))
notifyChangeAttribute =
GetLogAttribute(type.GetProperty(methodInfo.Name.Substring(methodInfo.Name.IndexOf("_")
+ 1)));
else
notifyChangeAttribute = GetLogAttribute(methodInfo);
return notifyChangeAttribute != null;
}
private static NotifyChangeAttribute
GetLogAttribute(ICustomAttributeProvider memberInfo)
{
object[] logAttribute = memberInfo.GetCustomAttributes(typeof
(NotifyChangeAttribute), true);
if (logAttribute.Length == 0)
return null;
return (NotifyChangeAttribute) logAttribute[0];
}
On Aug 13, 4:51 pm, omer katz <[email protected]> wrote:
> This happens because the proxy inherits the type and you loose all
> attributes.
> Refer to the original class and get the method by methodInfo.name
>
> 2010/8/12 PB <[email protected]>
>
>
>
> > thanks. you have been a great help. hv another question. as I did not
> > want every member of the class to be intercepted, i created following
> > hook that inspects the attribute on the members. now issue is
> > attributes on methods are identified, but not on the property.
>
> > public class AutoNotifyPropertyChangedProxyGenerationHook :
> > IProxyGenerationHook
> > {
> > public void MethodsInspected() {}
> > public void NonProxyableMemberNotification(Type type, MemberInfo
> > memberInfo) {}
>
> > public bool ShouldInterceptMethod(Type type, MethodInfo
> > methodInfo)
> > {
> > return GetAttribute(methodInfo) != null;
> > }
>
> > private static NotifyChangeAttribute GetAttribute(MethodInfo
> > methodInfo)
> > {
> > object[] logAttribute =
> > methodInfo.GetCustomAttributes(typeof(NotifyChangeAttribute), true);
> > if (logAttribute.Length == 0)
> > {
> > return null;
> > }
> > else
> > {
> > return (NotifyChangeAttribute)logAttribute[0];
> > }
> > }
> > }
>
> > public interface IViewModel
> > {
> > [NotifyChange] // this is not identified in the hook.
> > string Name { get; set; }
>
> > [NotifyChange] // this is not identified in the hook.
> > string Age { get; set; }
>
> > [NotifyChange] // this does get indentified
> > void SomeMethod();
> > }
>
> > Could you please help. thanks
>
> > On Aug 11, 9:15 am, Krzysztof Koźmic <[email protected]>
> > wrote:
> > > http://stw.castleproject.org/Windsor.Installers.ashx
>
> > > helper method as in a private method that you'll wrap your repetitive
> > > code with.
>
> > > Krzysztof
>
> > > On 11/08/2010 9:08 AM,PBwrote:
>
> > > > could you please provide some reference on writing a helper method in
> > > > the installer? by installer do you mean application package? thanks
>
> > > > On Aug 10, 3:15 pm, Krzysztof Koźmic<[email protected]>
> > > > wrote:
> > > >> Yes and no.
>
> > > >> Yes there's no OOTB dedicated hook in the container for this.
> > > >> No, you don't have to specify that manually for each and every
> > > >> component. Like I said before you can write a helper method in your
> > > >> installer that will do it, or build a ComponentModel construction
> > > >> contributor, although I'd rather suggest going with helper method.
>
> > > >> Krzysztof
>
> > > >> On 10/08/2010 3:10 PM,PBwrote:
>
> > > >>> So you mean if I have 50 classes that I want to provide same logger,
> > > >>> I’ll have to explicitly register the same interceptor and hook for
> > > >>> every class
> > > >>> container.Register(Component.For<LogInterceptor>(),
> > > >>> Component.For<LoggingProxyGenerationHook>(),
> > > >>> Component.For<ILogger>().ImplementedBy<Logger>(),
>
> > Component.For<IFoo>().ImplementedBy<Foo>().Interceptors<LogInterceptor>().P¬roxy.Hook(
> > > >>> h => h.Service<LoggingProxyGenerationHook>()),
>
> > Component.For<IMath>().ImplementedBy<Math>().Interceptors<LogInterceptor>().
> > > >>> Proxy.Hook(h => h.Service<LoggingProxyGenerationHook>()),
>
> > Component.For<ICar>().ImplementedBy<Car>().Interceptors<LogInterceptor>().
> > > >>> Proxy.Hook(h => h.Service<LoggingProxyGenerationHook>())
> > > >>> );
> > > >>> Can’t we configure the container to use the interceptor for all the
> > > >>> classes that get resolved by the container? Wouldn’t it be easier and
> > > >>> cleaner that way, you don’t have to be repetitive.
> > > >>> I can’t remember exactly, but there was mention of sometime similar
> > by
> > > >>> creating a facility and registering it, something along those lines?
> > > >>> Regards
> > > >>> On Aug 10, 7:50 am, Krzysztof Koźmic<[email protected]>
> > > >>> wrote:
> > > >>>> No you cant - adding interceptor is a fine grained task and you
> > seldom
> > > >>>> want to apply them globally.
> > > >>>> If you do though - nothis is stopping you from adding a helper
> > method in
> > > >>>> your installer and registering through it.
> > > >>>> HTH
> > > >>>> Krzysztof
> > > >>>> On 9/08/2010 11:36 PM,PBwrote:
> > > >>>>> Finally got it working, have couple of questions though.
> > > >>>>> This is what I did:
> > > >>>>> public class LoggingProxyGenerationHook : IProxyGenerationHook
> > > >>>>> {
> > > >>>>> public bool ShouldInterceptMethod(Type type, MethodInfo
> > > >>>>> methodInfo)
> > > >>>>> {
> > > >>>>> return GetLogAttribute(methodInfo) != null;
> > > >>>>> }
> > > >>>>> private static LogAttribute
> > > >>>>> GetLogAttribute(ICustomAttributeProvider methodInfo)
> > > >>>>> {
> > > >>>>> object[] logAttribute =
> > methodInfo.GetCustomAttributes(typeof
> > > >>>>> (LogAttribute), true);
> > > >>>>> if (logAttribute.Length == 0)
> > > >>>>> {
> > > >>>>> return null;
> > > >>>>> }
> > > >>>>> else
> > > >>>>> {
> > > >>>>> return (LogAttribute) logAttribute[0];
> > > >>>>> }
> > > >>>>> }
> > > >>>>> }
> > > >>>>> container.Register(Component.For<LogInterceptor>(),
> > > >>>>> Component.For<LoggingProxyGenerationHook>(),
>
> > Component.For<ILogger>().ImplementedBy<Logger>(),
>
> > Component.For<IFoo>().ImplementedBy<Foo>().Interceptors<LogInterceptor>().Proxy.Hook(
> > > >>>>> h =>
> > > >>>>> h.Service<LoggingProxyGenerationHook>()));
> > > >>>>> The above code registers LogInterceptor for implementation of IFoo.
> > > >>>>> What if I want to use the interceptor with every interface
> > > >>>>> implementation that is instantiated through the container. Is there
> > a
> > > >>>>> global level where we can register the interceptor and the proxy
> > hook,
> > > >>>>> in order to avoid the repetitive task of registeration for each
> > > >>>>> interface.
> > > >>>>> Thanks
> > > >>>>> On Jul 23, 1:25 pm, Krzysztof Koźmic<[email protected]>
> > > >>>>> wrote:
> > > >>>>>> Looks like it's missing from v2.1
> > > >>>>>> You'd have to write custom descriptor and call AddDescriptor(new
> > > >>>>>> MyDescriptor(new SomeHook()));
> > > >>>>>> In the descriptor you override the ApplyToModel method like this:
> > > >>>>>> ProxyUtil.ObtainProxyOptions(model, true).Hook= this.hook;
> > > >>>>>> or you can upgrade to v2.5 which has that.
> > > >>>>>> HTH,
> > > >>>>>> Krzysztof
> > > >>>>>> On 23/07/2010 1:08 PM,PBwrote:
> > > >>>>>>> do you know what that way would be?
> > > >>>>>>> On Jul 20, 12:54 pm, Krzysztof Koźmic<[email protected]
>
> > > >>>>>>> wrote:
> > > >>>>>>>> Correct, that was added recently. I think there was some way of
> > > >>>>>>>> specifying hook in v2.1.1 though
> > > >>>>>>>> On 20/07/2010 12:52 PM,PBwrote:
> > > >>>>>>>>> Hi
> > > >>>>>>>>> Thanks for reply. I am using v2.1.1 and there is no
> > implementation of
> > > >>>>>>>>> Hook
> > > >>>>>>>>> Proxy.Hook(h => h.Service<ProxyNothingHook>()
> > > >>>>>>>>> Regards
> > > >>>>>>>>> On Jul 13, 9:58 pm, Krzysztof Koźmic<
> > [email protected]>
> > > >>>>>>>>> wrote:
> > > >>>>>>>>>> You don't register PGO, but you can configure all relevant
> > aspects of
> > > >>>>>>>>>> the proxy mechanism.
> > > >>>>>>>>>> In trunk version of Windsor you can do:
> > > >>>>>>>>>> var interceptor = new ResultModifierInterceptor(5);
>
> > container.Register(Component.For<ResultModifierInterceptor>().Instance(interceptor),
>
> > Component.For<ProxyNothingHook>(),
> > > >>>>>>>>>> Component.For<ICalcService>()
>
> > .ImplementedBy<CalculatorService>()
>
> > .Interceptors<ResultModifierInterceptor>()
> > > >>>>>>>>>> .Proxy.Hook(h =>
> > h.Service<ProxyNothingHook>()));
> > > >>>>>>>>>> var calc = container.Resolve<ICalcService>();
> > > >>>>>>>>>> Assert.AreEqual(4,calc.Sum(2,2));
> > > >>>>>>>>>> In version v2.1.1 something similar although you can provide
> > hook only
> > > >>>>>>>>>> as an instance IIRC.
> > > >>>>>>>>>> Version 2.5 beta1 does not have it which is my omission. Beta2
> > and final
> > > >>>>>>>>>> version will work like the sample above
> > > >>>>>>>>>> Krzysztof
> > > >>>>>>>>>> On 13/07/2010 9:53 PM,PBwrote:
> > > >>>>>>>>>>> I came across this article of your on Selecting which methods
> > to
> > > >>>>>>>>>>> intercept which shows an implementation of
> > ProxyGenerationOptions with
> > > >>>>>>>>>>> ProxyGenerationHook. I am using windsor container and
> > register types
> > > >>>>>>>>>>> as follow
> > > >>>>>>>>>>> IWindsorContainer container = new WindsorContainer();
> > > >>>>>>>>>>> container.Register(
>
> > Component.For<IFoo>().ImplementedBy<Foo>(),
>
> > Component.For<ILogger>().ImplementedBy<Logger>(),
>
> > Component.For<LogInterceptor>().LifeStyle.Transient);
> > > >>>>>>>>>>> could you please indicate how can i register
> > ProxyGenerationOptions
> > > >>>>>>>>>>> using windsor container. Thanks
>
> >http://kozmic.pl/archive/2009/01/17/castle-dynamic-proxy-tutorial-par...
> > > >>>>>>>>>>> On Jul 10, 11:15 pm, Krzysztof Koźmic<
> > [email protected]>
> > > >>>>>>>>>>> wrote:
> > > >>>>>>>>>>>> using either IProxyGenerationHook, or IIntreceptorSelector
>
> > Component.For<IFoo>().ImplementedBy<Foo>().Interceptors<SomeInterceptor,
> > > >>>>>>>>>>>> SomeOtherInterceptor>()
> > > >>>>>>>>>>>> 2010/7/10PB<[email protected]>:
> > > >>>>>>>>>>>>> thanks. then how can i prevent a method from being
> > intercepted for
> > > >>>>>>>>>>>>> interfaces?
> > > >>>>>>>>>>>>> regards.
> > > >>>>>>>>>>>>> On Jul 9, 8:05 pm, Krzysztof Koźmic<
> > [email protected]>
> > > >>>>>>>>>>>>> wrote:
> > > >>>>>>>>>>>>>> You're mixing stuff up here. Attributes and being virtual
> > are unrelated.
> > > >>>>>>>>>>>>>> Class components need to have methods virtual to be
> > interceptable.
> > > >>>>>>>>>>>>>> Interfaces are virtual by definition.
> > > >>>>>>>>>>>>>> You don't need attributes
>
> ...
>
> read more »- Hide quoted text -
>
> - Show quoted text -
--
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.