Global properties aren't something that lots of C# developers use often, however every application takes them, whether you are careful of them or not. Global properties are a special way to store meta information for a special assembly or module.
Global properties are split in to two categories: assembly & module. A module is a various code file & an assembly is a single DLL or executable. An assembly can contain several modules too a module can contain several categories.
When you generate a new project in Visual Studio, usually it creates a file for you called AssemblyInfo.cs. This file uses global attributes to store important information about the assembly like version and copyright information. Below is an excerpt from an example AssemblyInfo.cs file.
In order to make our own |tradition properties like these, the first thing we are running to demand to do is make a class that extends Property to store the data.
using System;
namespace GlobalAttributes
{
public class MyCustomAttribute : Attribute
{
public string MyValue { get; set; }
}
}
Here I created a basic custom attribute. It's four property, MyValue, which holds a string. Now let's see how to use the attribute in our own code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlobalAttributes;
[assembly: MyCustomAttribute(MyValue="My assembly attribute value.")]
[module: MyCustomAttribute(MyValue = "My module attribute value.")]
namespace GlobalAttributes
{
class Program
{
static void Main(string[] args)
{
}
}
}
0 comments:
Post a Comment