CsharpSummer - Reflection
CsharpSummer - Reflection
NET
Pavel Jeek
Based on University of Linz .NET presentations. University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)
Reflection
Getting meta-information about assemblies, modules and types Getting meta-information about the members of a type Dynamic creation of instances of a type at run-time Search for methods and their dynamic invocation at run-time Accessing values of properties and fields of an object Design of new types at run time - namespace System.Reflection.Emit
Class Assembly
Class Assembly loads assemblies and their meta-data Provides access to its meta-data
public class Assembly { public static Assembly Load(string name); public virtual string FullName {get;} public virtual string Location {get;} public virtual MethodInfo EntryPoint {get;} public Module[] GetModules(); public virtual Type[] GetTypes(); public virtual Type GetType(string typeName); public object CreateInstance(string typeName); ... } Loading an assembly Name, storage location, entry point of the assembly Getting modules and all in the assembly defined types Getting type with name typeName Creation of an object of type typeName
Class Type
Type used for meta-description of all types in the run-time system Provides access to the meta-information about its members
public abstract class Type : MemberInfo, IReflect { public abstract string FullName {get;}; public abstract Type BaseType {get;}; public Type[] GetInterfaces(); public bool IsAbstract {get;}; public bool IsClass {get;}; public bool IsPublic {get;}; public ConstructorInfo[] GetConstructors(); public virtual EventInfo[] GetEvents(); public FieldInfo[] GetFields(); public MethodInfo[] GetMethods(); public PropertyInfo[] GetProperties(); ... Type name Direct base type List of implemented interfaces
Properties of type
Example Reflection
C# program "HelloWorld"
namespace Hello { using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("HelloWorld"); } public override string ToString() { return "Example HelloWorld"; }
csc HelloWorld.cs
HelloWorld.exe
Example Reflection
Hello.HelloWorld
Positional parameter = parameter of the attribute's constructor Name parameter = a property of the attribute
Valid variants:
[Obsolete] [Obsolete("some Message")] [Obsolete("some Message", false)] [Obsolete("some Message", IsError=false)] // Message == "", IsError == false // IsError == false
AttributeUsage
AttributeUsage describes how user-defined attributes are to be used
public class AttributeUsageAttribute : Attribute { public AttributeUsageAttribute (AttributeTargets validOn) {...} public bool AllowMultiple { get; set; } // default: false public bool Inherited { get; set; } // default: false }
Usage
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple=false)] public class MyAttribute : Attribute { ... }
Usage
[Comment("This is a demo class for Attributes", Author="XX")] class C { ... }
Example Reflection