00:00

Understanding Runtime Types and Attributes in .NET

This content delves into the hierarchical structure of classes, assemblies, and types in the runtime system of .NET. It explores how to access meta-information about members, constructors, events, fields, methods, properties, interfaces, and more. Additionally, it discusses the use of attributes with parameters, such as the ObsoleteAttribute and custom attribute creation. The post concludes with insights on querying attributes at runtime.

canturri
Télécharger la présentation

Understanding Runtime Types and Attributes in .NET

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 Class Hierarchy Assembly GetTypes() * BaseType Type Interfaces * GetFields() * FieldInfo GetMethods() MethodInfo * MethodBase MemberInfo GetConstructors() * ConstructorInfo GetProperties() * PropertyInfo GetEvents() * EventInfo

  2. 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(); • Type name • Direct base type • List of implemented interfaces public bool IsAbstract {get;}; public bool IsClass {get;}; public bool IsPublic {get;}; … • Properties of type • Getting constructors, events, fields, methods, properties • Optionally parameterized by BindingFlags public ConstructorInfo[] GetConstructors(); public virtual EventInfo[] GetEvents(); public FieldInfo[] GetFields(); public MethodInfo[] GetMethods(); public PropertyInfo[] GetProperties(); ...

  3. 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(nameof(IMyPlugin)) != null) { IMyPlugin p = (IMyPlugin) Activator.CreateInstance(t); // add p to list of all installed plug-ins } } } }

  4. Attributes with Parameters Example [Obsolete("Use class C1 instead", IsError=true)] public class C {...} // causes compiler message saying // 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 { public string Message { get; } public bool IsError { get; set; } public ObsoleteAttribute() {...} public ObsoleteAttribute(string msg) {...} public ObsoleteAttribute(string msg, bool error) {...} } // class name ends with "Attribute" // but can be used as "Obsolete" Valid variants: [Obsolete] [Obsolete("some Message")] [Obsolete("some Message", false)] [Obsolete("some Message", IsError=false)] // Message == "", IsError == false // IsError == false values must be constants

  5. AttributeUsage AttributeUsage describes how user-defined attributes are to be used public class AttributeUsageAttribute : Attribute { public AttributeUsageAttribute (AttributeTargets validOn) {...} public bool AllowMultiple { get; set; } public bool Inherited { get; set; } } // default: false // default: false Usage [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple=false)] public class MyAttribute : Attribute { ... }

  6. 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); } } }

More Related