none
ICustomAttributeProvider doesn't works? RRS feed

  • Question

  • Test code below:

        public class MyService : ICustomAttributeProvider
        {
            public virtual object[] GetCustomAttributes(bool inherit)
            {
                return new object[1] { new ServiceContractAttribute() };
            }

            public virtual object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                return new object[1] { new ServiceContractAttribute() };
            }

            public virtual bool IsDefined(Type attributeType, bool inherit)
            {
                return true;
            }
        }

    typeof(MyService).GetCustomAttribute(typeof(ServiceContractAttribute))

    return null?


    • Edited by Allen Liu Tuesday, February 26, 2013 2:36 AM
    Tuesday, February 26, 2013 2:28 AM

Answers

  • Hi Allen,

    >>GetCustomAttribute(...) return first Attribute match the type, and it should ok.

    I don't believe it should work. There is no such method in System.Type: http://msdn.microsoft.com/en-us/library/system.type.aspx 

    Anyway, let's back to your original question: Your class MyService indeed implements the interface. But when you still call the GetCustomAttributes method of Type class, not MyService, because you have code:

    typeof(MyService)

    The above code returns a Type instance. Please compare the follow two code snippet, you can get better what I mean:

    One:

                Type t = typeof(MyService);
                object[] a = t.GetCustomAttributes(typeof(ServiceContractAttribute), true);
                Console.WriteLine(a.Length);

    Two:

                MyService ms = new MyService(typeof(MyService));
                object[] an = ms.GetCustomAttributes(typeof(ServiceContractAttribute), true);
                Console.WriteLine(an.Length);
    
    
    
        public class MyService : ICustomAttributeProvider
        {
            public virtual object[] GetCustomAttributes(bool inherit)
            {
                return new object[1] 
                { new ServiceContractAttribute()
                };
            }
    
            public object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                return new object[1] { new ServiceContractAttribute() };
            }
    
            public virtual bool IsDefined(Type attributeType, bool inherit)
            {
                return true;
            }
    
            public MyService(Type t)
            {
            }
        }

    I hope this will helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Allen Liu Friday, March 1, 2013 5:05 AM
    Thursday, February 28, 2013 7:53 AM
    Moderator

All replies

  • Hi Allen,

    Welcome to the MSDN Forum.

    Based on your above code, there is no overloads method

    GetCustomAttributes(Type attributeType)

    And it seems there is a typo in this statement (note the bold):

    typeof(MyService).GetCustomAttributes(typeof(ServiceContractAttribute))

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 27, 2013 6:09 AM
    Moderator
  • Hi Mik.,

    Thanks for your reply!

    GetCustomAttribute(...) return first Attribute match the type, and it should ok.

    var attrs = typeof(MyService).GetCustomAttributes(typeof(ServiceContractAttribute));

    will return 0 instance either.

    What I want to do is get custom attribute in runtime instead of mark in class level, does ICutomAttributes works for this?

    Wednesday, February 27, 2013 3:27 PM
  • Hi Allen,

    >>GetCustomAttribute(...) return first Attribute match the type, and it should ok.

    I don't believe it should work. There is no such method in System.Type: http://msdn.microsoft.com/en-us/library/system.type.aspx 

    Anyway, let's back to your original question: Your class MyService indeed implements the interface. But when you still call the GetCustomAttributes method of Type class, not MyService, because you have code:

    typeof(MyService)

    The above code returns a Type instance. Please compare the follow two code snippet, you can get better what I mean:

    One:

                Type t = typeof(MyService);
                object[] a = t.GetCustomAttributes(typeof(ServiceContractAttribute), true);
                Console.WriteLine(a.Length);

    Two:

                MyService ms = new MyService(typeof(MyService));
                object[] an = ms.GetCustomAttributes(typeof(ServiceContractAttribute), true);
                Console.WriteLine(an.Length);
    
    
    
        public class MyService : ICustomAttributeProvider
        {
            public virtual object[] GetCustomAttributes(bool inherit)
            {
                return new object[1] 
                { new ServiceContractAttribute()
                };
            }
    
            public object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                return new object[1] { new ServiceContractAttribute() };
            }
    
            public virtual bool IsDefined(Type attributeType, bool inherit)
            {
                return true;
            }
    
            public MyService(Type t)
            {
            }
        }

    I hope this will helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Allen Liu Friday, March 1, 2013 5:05 AM
    Thursday, February 28, 2013 7:53 AM
    Moderator
  • Thanks again! Mike

    I was thought that typeof or GetType will return type object that has replace GetCustomAttributes method by user's implement.

    Friday, March 1, 2013 5:04 AM