slight change instead of using IndexOf("_") + 1 you can do this
const string propertyPrefix = "set_";
methodInfo.Name.Substring(propertyPrefix.Length)
On Aug 13, 7:23 pm, PB <[email protected]> wrote:
> 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
>
> ...
>
> 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.