Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced .NET Programming I 8th Lecture

Similar presentations


Presentation on theme: "Advanced .NET Programming I 8th Lecture"— Presentation transcript:

1 Advanced .NET Programming I 8th Lecture
Pavel Ježek Some of the slides are based on University of Linz .NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (

2 Reflection Permits access to meta-information of types at run-time
System.Reflection allows: 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

3 Reflection Class Hierarchy
Assembly GetTypes() Type * BaseType * Interfaces GetFields() * FieldInfo MemberInfo GetMethods() * MethodInfo MethodBase GetConstructors() * ConstructorInfo GetProperties() * PropertyInfo GetEvents() * EventInfo

4 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 Getting constructors, events, fields, methods, properties Optionally parameterized by BindingFlags

5 Example: Handling plug-ins
“Plug-in” is any class implementing IMyPlugin interface string[] files = Directory.GetFiles(path, "*.dll"); // returns full paths foreach (string f in files) { Assembly a = Assembly.LoadFile(f); Type[] types = a.GetTypes(); foreach (Type t in types) { if (t.IsClass) { if (t.GetInterface(“IMyPlugin”) != null) { IMyPlugin p = (IMyPlugin) Activator.CreateInstance(t); // add p to list of all installed plug-ins }

6 Attributes with Parameters
name parameters come after pos. parameters positional parameter Example [Obsolete("Use class C1 instead", IsError=true)] // causes compiler message saying public class C {...} // that C is obsolete Positional parameter = parameter of the attribute's constructor Name parameter = a property of the attribute Attributes are declared as classes public class ObsoleteAttribute : Attribute { // class name ends with "Attribute" public string Message { get; } // but can be used as "Obsolete" public bool IsError { get; set; } public ObsoleteAttribute() {...} public ObsoleteAttribute(string msg) {...} public ObsoleteAttribute(string msg, bool error) {...} } values must be constants Valid variants: [Obsolete] // Message == "", IsError == false [Obsolete("some Message")] // IsError == false [Obsolete("some Message", false)] [Obsolete("some Message", IsError=false)]

7 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 { ... }

8 Defining Your Own Attributes
Declaration [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited=true)] class CommentAttribute : Attribute { string text, author; public string Text { get {return text;} } public string Author { get {return author;} set {author = value;} } public Comment (string text) { this.text = text; author ="HM"; } } Usage [Comment("This is a demo class for Attributes", Author="XX")] class C { ... } Querying the attribute at runtime class Attributes { static void Main() { Type t = typeof(C); object[] a = t.GetCustomAttributes(typeof(CommentAttribute), true); foreach (CommentAttribute ca in a) { Console.WriteLine(ca.Text + ", " + ca.Author); }

9 Reflection Example Print all existing types in a given assembly
Type[] types = a.GetTypes(); foreach (Type t in types) Console.WriteLine(t.FullName); Print all existing methods of a given type Type hw = a.GetType("Hello.HelloWorld"); MethodInfo[] methods = hw.GetMethods(); foreach (MethodInfo m in methods) Console.WriteLine(m.Name); Hello.HelloWorld GetType ToString Equals GetHashCode

10 Reflection Example Create a new instance of a given type
Assembly a = Assembly.Load("HelloWorld"); object o = a.CreateInstance("Hello.HelloWorld"); Get method ToString(), which has no parameters Invoke the method Type hw = a.GetType("Hello.HelloWorld"); MethodInfo mi = hw.GetMethod("ToString"); object retVal = mi.Invoke(o, null);

11 Reflection: Accessing Private Fields
using System.Reflection; class HelperClass { private int privateData; public int PublicData { get { return privateData; } set { privateData = value; } } class Program { static void Main(string[] args) { HelperClass hc = new HelperClass(); hc.PublicData = 123; Type type = hc.GetType(); FieldInfo fi = type.GetField("privateData", BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine(fi.GetValue(hc)); fi.SetValue(hc, 456); Console.WriteLine(hc.PublicData);

12 Reflection: Accessing Private Fields
using System.Reflection; class HelperClass { private int privateData; public int PublicData { get { return privateData; } set { privateData = value; } } class Program { static void Main(string[] args) { HelperClass hc = new HelperClass(); hc.PublicData = 123; Type type = hc.GetType(); FieldInfo fi = type.GetField("privateData", BindingFlags.Instance | BindingFlags.NonPublic); Console.WriteLine(fi.GetValue(hc)); fi.SetValue(hc, 456); Console.WriteLine(hc.PublicData); WARNING: SLOW & DANGEROUS!!!

13 MEF

14 Lambda Expressions as Delegates
When assigned to a delegate, equivalent code of an anonymous method is generated at compile time! value => (value + 2) * 10 Func<int, int> f = Compile time generation IL_0000: ldarg.0 IL_0001: ldc.i4.2 IL_0002: add IL_0003: ldc.i4.s 10 IL_0005: mul IL_0006: stloc.0 IL_0007: br.s IL_0009 IL_0009: ldloc.0 IL_000a: ret

15 Lambda Expressions as Expression Trees
Permit lambda expressions to be represented as data structures instead of executable code Lambda expression convertible to delegate D (assignment causes code generation) is also convertible to expression tree (abstract syntax tree) of type System.Linq.Expressions.Expression<D> (assignment causes expression tree generation – compile time generation of code, that creates the expression tree [class instances] at runtime) Expression trees are immutable value => (value + 2) * 10 Func<int, int> f = Expression<Func<int, int>> e = Compile time generation Compile time generation IL_0000: ldarg.0 IL_0001: ldc.i4.2 IL_0002: add IL_0003: ldc.i4.s 10 IL_0005: mul IL_0006: stloc.0 IL_0007: br.s IL_0009 IL_0009: ldloc.0 IL_000a: ret new LambdaExpression( new BinaryExpression( ParameterExpression(“value”) ConstantExpression(2) ) ConstantExpression(10)

16 Expression Trees Classes inheriting from Expression (since .NET 3.5):
System.Linq.Expressions.BinaryExpression System.Linq.Expressions.ConditionalExpression System.Linq.Expressions.ConstantExpression System.Linq.Expressions.InvocationExpression System.Linq.Expressions.LambdaExpression System.Linq.Expressions.MemberExpression System.Linq.Expressions.MethodCallExpression System.Linq.Expressions.NewExpression System.Linq.Expressions.NewArrayExpression System.Linq.Expressions.MemberInitExpression System.Linq.Expressions.ListInitExpression System.Linq.Expressions.ParameterExpression System.Linq.Expressions.TypeBinaryExpression System.Linq.Expressions.UnaryExpression New classes inheriting from Expression (since .NET 4.0): System.Linq.Expressions.BlockExpression System.Linq.Expressions.LoopExpression System.Linq.Expressions.TryExpression

17 Expression Trees and LINQ

18 Lambda Expressions as Expression Trees
value => (value + 2) * 10 Func<int, int> f = Expression<Func<int, int>> e = Compile time generation Compile time generation IL_0000: ldarg.0 IL_0001: ldc.i4.2 IL_0002: add IL_0003: ldc.i4.s 10 IL_0005: mul IL_0006: stloc.0 IL_0007: br.s IL_0009 IL_0009: ldloc.0 IL_000a: ret new LambdaExpression( new BinaryExpression( ParameterExpression(“value”) ConstantExpression(2) ) ConstantExpression(10) Runtime time generation by JIT machine code (e.g. x86) (code actually executed by real CPU)

19 Expression Trees to Dynamic Methods (via Implicit Reflection.Emit)
Runtime generation of CIL code of a dynamic method from expression tree instance: value => (value + 2) * 10 Func<int, int> f = Expression<Func<int, int>> e = Compile time generation Compile time generation IL_0000: ldarg.0 IL_0001: ldc.i4.2 IL_0002: add IL_0003: ldc.i4.s 10 IL_0005: mul IL_0006: stloc.0 IL_0007: br.s IL_0009 IL_0009: ldloc.0 IL_000a: ret new LambdaExpression( new BinaryExpression( ParameterExpression(“value”) ConstantExpression(2) ) ConstantExpression(10) Runtime time generation f = e.Compile(); Runtime time generation by JIT machine code (e.g. x86) (code actually executed by real CPU)


Download ppt "Advanced .NET Programming I 8th Lecture"

Similar presentations


Ads by Google