CategoryFind and create types at runtime by using reflection

Assembly class

Assembly class allows us to:

  • Load assemblies
  • Explore metadata and types
  • Create instance of types

Easiest way to load assembly is by load them using following methods :

GetAssembly (requires Type variable)

GetCallingAssembly (returns Assembly from which this method has been called)

These two methods have Assembly return type. After acquiring Assembly variable we can use it to get types that we want using GetType method.

Let’s explore Assembly class:

 

MethodInfo

Type’s class GetMethods method returns us with a array of MethodInfo objects. Similarly GetMethod method provided with a name of Method returns us a  MethodInfo object. It allows us to access a lot of information and interaction concerning specific Method, such as:

Check if a method is a constructor,

Access modifier of a method,

Method parameters,

Method name,

Invoking/Calling method

And many more…

Example:

We will continue our voyage with Reflection in next lesson about Assembly class.

 

Type class

Type class

Type class is an essential class to use while working with a Reflection. Using Type class we can access assembly, module or type run-time information. Accessing these information is simple:

First approach is to use Type’s GetType method. This approach is used if we do not have assembly loaded in our solution. Notice that Type.GetType expects string value, also you need to provide fully qualified name (namespace.typeName).

Second approach would be using typeof operator. Usage is very simple, type keyword typeof followed by Type name in parentheses.

Third approach is convenient if you already have instance of a type. Simply use GetType method. In C# everything is inheriting Object class there fore everything is a class and thus inherits GetType method.

I have acquired type object. What do i do with it now?

Now you can start to use Reflection 🙂 Next post will explain how you can access types Properties.

 

Attributes

.ttributes have mostly decorative impact on your applications and are used mostly by Reflection. Attributes can be applied almost everywhere in your code warring from get accessor to entire assembly.

How to create your custom Attributes?

You create it like a class, but it is important that a class inherits from a Attribute class or a class that inherits from a Attribute class. You should also apply System.AttributeUsage over your class, where is decided how this attributes class can be used. Attributes are assigned to a class, method… by writing square brackets inside which is written attributes name, also if your attribute is named MyCustomAttribute, you need not to write attribute in your Attributes name because compiler will know what to do.

So let’s create our attribute:

This Attribute can be applied almost everywhere in your code. List of possible usages is here:

  • Assembly
  • Module
  • Class
  • Struct
  • Enum
  • Constructor
  • Method
  • Property
  • Field
  • Type
  • Event
  • Interface
  • Parameter
  • Delegate
  • ReturnValue
  • GenericParameter

 

We can set constraints to our constructor, so for example that it can be only applied to Methods and Properties:

Note that we used | character to separate two possible usages.

 

There is also feature to use same Attribute multiple times on a same target, that feature can be turned on or off using Allow multiple option (default value is false).

 

If we want to enable or disable that the attribute is inherited by classes that are derived from a class that is attributed we can use Inherited option (default is true).

 

How to apply Attributes?

Simply, put above method,class,property…  Attributes name inside square brackets

 

Common Uses for Attributes

The following list is copied from MSDN Library:

  • Marking methods using the WebMethod attribute in Web services to indicate that the method should be callable over the SOAP protocol. For more information, see WebMethodAttribute.
  • Describing how to marshal method parameters when interoperating with native code. For more information, see MarshalAsAttribute.
  • Describing the COM properties for classes, methods, and interfaces.
  • Calling unmanaged code using the DllImportAttribute class.
  • Describing your assembly in terms of title, version, description, or trademark.
  • Describing which members of a class to serialize for persistence.
  • Describing how to map between class members and XML nodes for XML serialization.
  • Describing the security requirements for methods.
  • Specifying characteristics used to enforce security.
  • Controlling optimizations by the just-in-time (JIT) compiler so the code remains easy to debug.
  • Obtaining information about the caller to a method.

 

How to consume Attributes is explained in articles concerning Reflection.

 

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑