Using PropertyInfo to find out the property type

来源:互联网 发布:免费名师讲课软件 编辑:程序博客网 时间:2024/06/11 18:58

http://stackoverflow.com/questions/3723934/using-propertyinfo-to-find-out-the-property-type

https://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.propertytype.aspx

public bool ValidateData(object data){    foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())    {        if (propertyInfo.PropertyType == typeof(string))        {            string value = propertyInfo.GetValue(data, null);            if value is not OK            {                return false;            }        }    }                return true;}

using System;using System.Reflection;public class Employee{   private string _id;   public String FirstName { get; set; }   public String MiddleName { get; set; }   public String LastName  { get; set; }   public DateTime HireDate  { get; set; }   public String ID   {      get { return _id; }      set {         if (ID.Trim().Length != 9)            throw new ArgumentException("The ID is invalid");         _id = value;      }   }}public class Example{   public static void Main()   {      Type t = typeof(Employee);      Console.WriteLine("The {0} type has the following properties: ",                        t.Name);      foreach (var prop in t.GetProperties())         Console.WriteLine("   {0} ({1})", prop.Name,                           prop.PropertyType.Name);   }}// The example displays the following output: //       The Employee type has the following properties: //          FirstName (String) //          MiddleName (String) //          LastName (String) //          HireDate (DateTime) //          ID (String)

The following example loads an assembly given its file name or path.

Assembly SampleAssembly;SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");// Obtain a reference to a method known to exist in assembly.MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");// Obtain a reference to the parameters collection of the MethodInfo instance.ParameterInfo[] Params = Method.GetParameters();// Display information about method parameters. // Param = sParam1 //   Type = System.String //   Position = 0 //   Optional=False foreach (ParameterInfo Param in Params){    Console.WriteLine("Param=" + Param.Name.ToString());    Console.WriteLine("  Type=" + Param.ParameterType.ToString());    Console.WriteLine("  Position=" + Param.Position.ToString());    Console.WriteLine("  Optional=" + Param.IsOptional.ToString());}
This example shows how to use various reflection classes to analyze the metadata contained in an assembly.

https://msdn.microsoft.com/en-us/library/system.reflection.assemblyname(v=vs.110).aspx

using System;using System.Reflection;class Module1{    public static void Main()    {        // This variable holds the amount of indenting that          // should be used when displaying each line of information.        Int32 indent = 0;        // Display information about the EXE assembly.        Assembly a = typeof(Module1).Assembly;        Display(indent, "Assembly identity={0}", a.FullName);        Display(indent+1, "Codebase={0}", a.CodeBase);        // Display the set of assemblies our assemblies reference.        Display(indent, "Referenced assemblies:");        foreach (AssemblyName an in a.GetReferencedAssemblies() )        {             Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKey token={3}", an.Name, an.Version, an.CultureInfo.Name, (BitConverter.ToString (an.GetPublicKeyToken())));        }        Display(indent, "");        // Display information about each assembly loading into this AppDomain.         foreach (Assembly b in AppDomain.CurrentDomain.GetAssemblies())        {            Display(indent, "Assembly: {0}", b);            // Display information about each module of this assembly.             foreach ( Module m in b.GetModules(true) )            {                Display(indent+1, "Module: {0}", m.Name);            }            // Display information about each type exported from this assembly.            indent += 1;            foreach ( Type t in b.GetExportedTypes() )            {                Display(0, "");                Display(indent, "Type: {0}", t);                // For each type, show its members & their custom attributes.                indent += 1;                foreach (MemberInfo mi in t.GetMembers() )                {                    Display(indent, "Member: {0}", mi.Name);                    DisplayAttributes(indent, mi);                    // If the member is a method, display information about its parameters.                     if (mi.MemberType==MemberTypes.Method)                    {                        foreach ( ParameterInfo pi in ((MethodInfo) mi).GetParameters() )                        {                            Display(indent+1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);                        }                    }                    // If the member is a property, display information about the property's accessor methods.                     if (mi.MemberType==MemberTypes.Property)                    {                        foreach ( MethodInfo am in ((PropertyInfo) mi).GetAccessors() )                        {                            Display(indent+1, "Accessor method: {0}", am);                        }                    }                }                indent -= 1;            }            indent -= 1;        }    }    // Displays the custom attributes applied to the specified member.     public static void DisplayAttributes(Int32 indent, MemberInfo mi)    {        // Get the set of custom attributes; if none exist, just return.         object[] attrs = mi.GetCustomAttributes(false);        if (attrs.Length==0) {return;}        // Display the custom attributes applied to this member.        Display(indent+1, "Attributes:");        foreach ( object o in attrs )        {            Display(indent+2, "{0}", o.ToString());        }    }    // Display a formatted string indented by the specified amount.     public static void Display(Int32 indent, string format, params object[] param)     {        Console.Write(new string(' ', indent*2));        Console.WriteLine(format, param);    }}//The output shown below is abbreviated. // //Assembly identity=ReflectionCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null //  Codebase=file:///C:/Documents and Settings/test/My Documents/Visual Studio 2005/Projects/Reflection/Reflection/obj/Debug/Reflection.exe //Referenced assemblies: //  Name=mscorlib, Version=2.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89 // //Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll //  Module: mscorlib.dll // //  Type: System.Object //    Member: GetType //    Member: ToString //    Member: Equals //      Parameter: Type=System.Object, Name=obj //    Member: Equals //      Parameter: Type=System.Object, Name=objA //      Parameter: Type=System.Object, Name=objB //    Member: ReferenceEquals //      Attributes: //        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute //      Parameter: Type=System.Object, Name=objA //      Parameter: Type=System.Object, Name=objB //    Member: GetHashCode //    Member: .ctor //      Attributes: //        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute // //  Type: System.ICloneable //    Member: Clone // //  Type: System.Collections.IEnumerable //    Member: GetEnumerator //      Attributes: //        System.Runtime.InteropServices.DispIdAttribute // //  Type: System.Collections.ICollection //    Member: CopyTo //      Parameter: Type=System.Array, Name=array //      Parameter: Type=System.Int32, Name=index //    Member: get_Count //    Member: get_SyncRoot //    Member: get_IsSynchronized //    Member: Count //      Accessor method: Int32 get_Count() //    Member: SyncRoot //      Accessor method: System.Object get_SyncRoot() //    Member: IsSynchronized //      Accessor method: Boolean get_IsSynchronized() // //  Type: System.Collections.IList //    Member: get_Item //      Parameter: Type=System.Int32, Name=index //    Member: set_Item //      Parameter: Type=System.Int32, Name=index //      Parameter: Type=System.Object, Name=value //    Member: Add //      Parameter: Type=System.Object, Name=value //    Member: Contains //      Parameter: Type=System.Object, Name=value //    Member: Clear //    Member: get_IsReadOnly //    Member: get_IsFixedSize //    Member: IndexOf //      Parameter: Type=System.Object, Name=value //    Member: Insert //      Parameter: Type=System.Int32, Name=index //      Parameter: Type=System.Object, Name=value //    Member: Remove //      Parameter: Type=System.Object, Name=value //    Member: RemoveAt //      Parameter: Type=System.Int32, Name=index //    Member: Item //      Accessor method: System.Object get_Item(Int32) //      Accessor method: Void set_Item(Int32, System.Object) //    Member: IsReadOnly //      Accessor method: Boolean get_IsReadOnly() //    Member: IsFixedSize //      Accessor method: Boolean get_IsFixedSize() // //  Type: System.Array //    Member: IndexOf //      Parameter: Type=T[], Name=array //      Parameter: Type=T, Name=value //    Member: AsReadOnly //      Parameter: Type=T[], Name=array //    Member: Resize //      Attributes: //        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute //      Parameter: Type=T[]&, Name=array //      Parameter: Type=System.Int32, Name=newSize //    Member: BinarySearch //      Attributes: //        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute //      Parameter: Type=T[], Name=array //      Parameter: Type=T, Name=value //    Member: BinarySearch //      Attributes: //        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute //      Parameter: Type=T[], Name=array //      Parameter: Type=T, Name=value //      Parameter: Type=System.Collections.Generic.IComparer`1[T], Name=comparer




0 0
原创粉丝点击