1 / 24

Reflection and Attributes

Reflection and Attributes. Reflection. The Common Language Runtime makes type information ubiquitous, accessible, and extensible reflection allows anyone to see any type (barring security) reflection information never optional - all things are knowable

brant
Download Presentation

Reflection and Attributes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reflection and Attributes

  2. Reflection • The Common Language Runtime makes type information ubiquitous, accessible, and extensible • reflection allows anyone to see any type (barring security) • reflection information never optional - all things are knowable • type information extensible via custom attributes • enables CLR automation of numerous services • serialization, remoting, windows/web forms, web services • enables numerous kinds of tool development • documentation/code generation, automated testing, … • System.Reflection namespace root of library support

  3. The role of reflection

  4. Example: using reflection public static void GenerateSQL(Object obj) { Type type = obj.GetType(); Console.Write("create table {0} (", type.Name); boolneedsComma = false; foreach (FieldInfo field in type.GetFields()) { if (needsComma) Console.Write(", "); else needsComma = true; Console.Write("{0} {1}", field.Name, field.FieldType); } Console.WriteLine(")"); }

  5. System.Type • System.Type is the focal point of reflection • all objects and values are instances of types • can discover type of object or value using GetType method • can reference type by symbolic name using C# typeof keyword • types are themselves instances of type System.Type • inheritance/compatibility relationships traversable through System.Type

  6. Types and Type.GetType

  7. System.Type

  8. Using System.Type • All facets of a type are available at runtime • each kind of member has a reflection class that represents it • most types derive from System.Runtime.MemberInfo • can look up members by kind or globally • support for overloading and case-insensitive names for script/VB • can see non-accessible members if security allows

  9. Reflection and the CLR type system

  10. The reflection object model

  11. Example: walking every element in an assembly using System.Reflection; public static void ListAllMembers( string assemblyPath ) { Assembly assembly = Assembly.LoadFrom(assemblyPath); foreach( Type type in assembly.GetTypes() ) { foreach( MemberInfo member in type.GetMembers() ) { Console.WriteLine("{0}.{1}", type, member.Name); } } }

  12. Late binding • Types may be instantiated and/or members accessed in a late bound manner • instantiate type in memory, choosing constructor to call • read/write fields of an object • invoke methods • invoke property getters and setters • register/unregister for events • Late bound access slower than compile-time resolution • trading off performance for flexibility/extensibility

  13. CreateInstance • CreateInstance is the late-bound equivalent to operator new • methods of Activator and Assembly classes • former used with fully-specified type names • latter convenient when explicitly loading assemblies • Both… • allocate storage for new type instance • call specified constructor • return generic object reference • When coupled with interface-based member access, provides flexibility and performance

  14. Example: using Activator.CreateInstance interface ICowboy { void Draw(); } class Tex : ICowboy { ... } class Woody : ICowboy { ... } class App { static void Main() { Console.Write("Enter western type to use: "); string typeName = Console.ReadLine(); // Late-bound activation ICowboycb = (ICowboy) Activator.CreateInstance(Type.GetType(typeName)); // Early-bound member access: cb.Draw(); } }

  15. Example: using Assembly.CreateInstance woody.dll public class Woody : ICowboy { ... } icowboy.dll (reference by everyone) public interface ICowboy { void Draw(); } tex.dll public class Tex : ICowboy { ... } PluggableApp.exe class PluggableApp { static void Main() { Console.Write("Enter assembly to load: "); Assembly asm = Assembly.LoadFrom(Console.ReadLine()); Console.Write("Enter concrete type name: "); string typeName = Console.ReadLine(); ICowboy cb = (ICowboy)asm.CreateInstance(typeName); cb.Draw(); } }

  16. Constructor arguments and CreateInstance class Tex : ICowboy { public Tex( string name, intNumGuns ) { ... } } class App { static void Main() { object[] args = { "Jesse James", 4 }; ICowboycb = (ICowboy) Activator.CreateInstance(typeof(Tex), args); cb.Draw(); } }

  17. Late-bound member access • MemberInfo derivatives enable late-bound member access • much slower than CreateInstance+interface-based call pattern • typically reserved for scenarios where performance isn’t a goal • interactive forms designers (e.g.: Windows/Web Forms) • automated testing harnesses • custom scripting engines • Usage model • acquire Type object for target type • lookup member of interest (field, property, event, method) • use MemberInfo to read/write/invoke member on target object

  18. Late-bound member access // Call method: double Add( double, double ) double CallAdd( object target, double x, double y ) { Type type = target.GetType(); MethodInfo method = type.GetMethod("Add"); if( method != null ) { object[] args = { x, y }; object result = method.Invoke(target, args); return (double)result; } return(0); }

  19. Extending type information • CLR type information is extensible using custom attributes • allows user-defined aspects of a type to be visible via reflection • custom attributes are classes derived from System.Attribute • C# uses IDL-like syntax with [ ] prior to the definition of the target • can be comma-delimited or in independent [ ] blocks • attribute parameters passed by position or name • attributes can be applied to an assembly or module using special syntax • attributes discovered using IsDefined and GetCustomAttributes

  20. Example: specifying an attribute [assembly: Author("Larry")] // Applies to assembly [ Author("Moe", "Hi") ] // Applies to MyClass class MyClass { // Attribute applies to method f: [ Author("Curly", Contact="curly@stooges.com") ] void f() { Object obj = null; obj.ToString(); } }

  21. Example: defining a custom attribute using System; public class AuthorAttribute : Attribute { public string Name; public string Comment; public string Contact = ""; public AuthorAttribute( string n, string c ) { Name = n; Comment = c; } public AuthorAttribute( string n ) : this(n, "") { } }

  22. Example: retrieving custom attributes using System; using System.Reflection; void DocType( string asmName, string typeName ) { Type attrType = typeof(AuthorAttribute); Assembly asm = Assembly.LoadFrom(asmName); if( asm.IsDefined(attrType, false) ) { Console.WriteLine("Assembly has an author"); } object[] attrs = asm.GetCustomAttributes(attrType, false); foreach( AuthorAttribute author in attrs ) { Console.WriteLine("Name: " + author.Name); Console.WriteLine("Notes: " + author.Comment); Console.WriteLine("Contact info: " + author.Contact); } }

  23. Summary • Type information is easily accessible and ubiquitous • Type information allows you to do things at runtime • Type information allows you to do things at development-time • Type information is extensible via custom attributes

  24. For more information, please contact: Uladzimir Tsikhon Software Engineering Manager, Belarus Recourse Development Department EPAM Systems, Inc. Belarus, MinskPhone: +375(17) 2101662 ext 1756 Fax: +375(17) 2101168 Email: uladzimir_tsikhon@epam.com http://www.epam.com

More Related