TagReflection

Version assemblies

When talking about Version assemblies you would usually interact with AssemblyVersion, AssemblyFileVersion, AssemblyInformationalVersion Atrributes. Ability to successfully version assemblies is important while testing, debugging and deploying your assemblies. Why? Because it allows you and your team to inspect and debug same version of assemblies on one hand and to know what version of assemblies is are your customers using when talking about future requirements and current problems.

AssemblyVersion and AssemblyFileVersion

attributes

Can be manually located in your Solution explorer:

AssemblyInfo.cs

AssemblyInfo.cs file you can manually change values of a few Attributes. But now we will take look at AssemblyVersion and AssemblyFileVersion attributes. Both these Attributes provide default value in string format. Default values  should appear like this when you initially create project:

Number values inside string have specific meaning:

  1. value -> major value. Value should be changed when changes to your program are noticeable, like major User interface and under the hood changes
  2. value -> minor. increment this value when adding new capabilities to your Application.
  3. value -> build number. Obviously this value should be incremented when running a build
  4. value -> revision. Value that you should increment when choosing which build version to deploy

Build number and revision can be automated by using asterisk(*). Just change strings too look like this and their values will be incremented automatically after each build:

If you are developing small scale application it is best to change minor and major values manually.

AssemblyVersion attribute is used by other assemblies. When value of this attribute is changed, all assemblies that reference it must change value to match current value to ensure that communication between them will proceed uninterrupted.

AssemblyFileVersion attribute is used for deployment (when you ship assemblies to end user). You always need to know which version of assembly is shipped to your customer so that you can assist them, detect bugs and plan future changes.

AssemblyInformationalVersion

Usually you would not like to bore or even worse scare customers by displaying version of your Application in format like 8.56.7520.1459. You will want to display assembly version in user friendly format like 8.56 RC or 8 Orange, or any other format that will appeal to end user. This is where AssemblyInformationalVersion steps in. We can declare AssemblyInformationalVersion inside AssemblyInfo.cs file.

Nice. Please show me how to access these values.

First of. make sure that your AssemblyInfo.cs files has following values:

And inside main Method put following code:

This post has thought you how to version assemblies and was first post in Manage assemblies topic. Now we will continue to topic on how to strong name your assembly.

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.

 

PropertyInfo

Type’s class GetProperties method returns us with a array of PropertyInfo objects. Similarly GetProperty method provided with a name of Property returns us a  PropertyInfo object. It allows us to access a lot of information and interaction concerning specific property, such as:

Does property have get or set accessor,

Properties Attributes,

Name,

Return type,

Allows us to change it’s value.

And so on…

Next time we will continue with MethodInfo 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.

 

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑