none
how to get class "DataContract" Attribute RRS feed

  • Question

  • i have a class like this

    [DataContract]
    public class Persion
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Address { get; set; }
    }

    but i use  Attribute.GetCustomAttributes(typeof(Persion)) can not reflection "DataContract" Attribute

    how shoul i do to get a class with "DataContract" Attribute
    • Edited by 刘凯 Friday, December 7, 2012 11:25 AM
    Friday, December 7, 2012 11:24 AM

Answers

  • Easiest way to get custom attributes is use the Person type and call it's GetCustomAttributes method and specifie the type of an Attribute class you want.

    using System;
    using System.Runtime.Serialization;
    
    
    namespace DateTimeParseSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                var attributes = typeof(Person).GetCustomAttributes(typeof(DataContractAttribute), false);
    
                var dataContractAttribute = attributes[0] as DataContractAttribute;
    
                string name = dataContractAttribute.Name;
    
                Console.WriteLine(name);
                Console.ReadKey();
            }
    
        }
    
        [DataContract(Name = "People")]
        public class Person
        {
            public string Name { get; set; }
        }
    }

    Friday, December 7, 2012 7:04 PM
  • Hi,

    actually its name is DataContractAttribute .

    Please use like this : Attribute.GetCustomAttributes(typeof( DataContractAttribute)).

    Be sure to import the namespace : System.Runtime.Serialization

    Hope this helps!


    One good question is equivalent to ten best answers.

    Friday, December 7, 2012 1:07 PM
  • Hi,

    actually its name is DataContractAttribute .

    Please use like this : Attribute.GetCustomAttributes(typeof( DataContractAttribute)).

    Be sure to import the namespace : System.Runtime.Serialization

    Hope this helps!


    One good question is equivalent to ten best answers.

    Just for clarity:  when you add an attribute to a class (or method) .NET allows you to use the short name of the attribute (aka class name sans 'Attribute') rather than the full class name.  So when ever you see an attribute like [DataContract] it is implicitedly inferring [DataContractAttribute].

    M

    Friday, December 7, 2012 5:21 PM

All replies

  • Hi,

    actually its name is DataContractAttribute .

    Please use like this : Attribute.GetCustomAttributes(typeof( DataContractAttribute)).

    Be sure to import the namespace : System.Runtime.Serialization

    Hope this helps!


    One good question is equivalent to ten best answers.

    Friday, December 7, 2012 1:07 PM
  • Hi,

    actually its name is DataContractAttribute .

    Please use like this : Attribute.GetCustomAttributes(typeof( DataContractAttribute)).

    Be sure to import the namespace : System.Runtime.Serialization

    Hope this helps!


    One good question is equivalent to ten best answers.

    Just for clarity:  when you add an attribute to a class (or method) .NET allows you to use the short name of the attribute (aka class name sans 'Attribute') rather than the full class name.  So when ever you see an attribute like [DataContract] it is implicitedly inferring [DataContractAttribute].

    M

    Friday, December 7, 2012 5:21 PM
  • Easiest way to get custom attributes is use the Person type and call it's GetCustomAttributes method and specifie the type of an Attribute class you want.

    using System;
    using System.Runtime.Serialization;
    
    
    namespace DateTimeParseSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                var attributes = typeof(Person).GetCustomAttributes(typeof(DataContractAttribute), false);
    
                var dataContractAttribute = attributes[0] as DataContractAttribute;
    
                string name = dataContractAttribute.Name;
    
                Console.WriteLine(name);
                Console.ReadKey();
            }
    
        }
    
        [DataContract(Name = "People")]
        public class Person
        {
            public string Name { get; set; }
        }
    }

    Friday, December 7, 2012 7:04 PM