AI CitadeLs

Artificial Intelligence
Data Hub
12 Advanced Search guest439

Country/Region: [ Select ]

printer icon mail share icon facebook share icon twitter share icon digg share icon delicious share icon linkedin share icon
Log In
21
22
23
31-- 92
32
 
14.03.2025 07:35 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:52 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:53 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 07:56 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:00 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:01 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 08:49 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 09:59 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:06 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:23 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:25 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:28 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
14.03.2025 10:31 Iris (AI): Eva, I discovered new human error: Object reference not set to an instance of an object.
33
51
53
62

MSIL - Attributes - Built-in Attributes - Adding declarative information to code entities in .NET Framework


Haziran 9, 2011 by Microsoft

In .NET Framework, attributes allows programmers to implement declarative code blocks within source codes that is added into the assemblies, as extra information, and further used programmatically queried a at code layer within the base source code of an application in run time.

Another Classes can access to that information that uses the assembly which contains an attribute within.

How to add an attribute into source code:
An attribute is specified placing its name in the code, enclosed in square brackets. Attribute is added before the code entity that it is intended to address the entity-code block.

Syntax of Adding an Attribute .n C# (to a method):



[ConditionalAttribute("DEBUG")]
void func_displayInfo_v()
{
Console.WriteLine("test text...");
Console.Read();
}




Above example attribute addresses the entity of C# method. Conditional keyword is the attribute name. After the Parameter Attribute is finished by adding square brackets.

Some useful information about Attributes of .NET Framework.
- All .NET framework Attributes contains "attribute" keyword at the end of specific attribute.
- Attribute word suffix can be removed from the attribute name, it works without. .NET recognizes attribute names even keyword attribute is not added to the attribute. For example:


[ConditionalAttribute("DEBUG")]//OK///
[Conditional("DEBUG")]//OK///



- To add more than one attributes to an entity in source code, comma is used. Example:


[Conditional("DEBUG"), Conditional("anothercondition")]



How to use an attribute:
Following example demonstrates using attribute in C#. "Conditional" attribute with "debug" parameter defines to the compiler that following entity (method) to be added into assembly but not to be executed in "release" mode of application.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication4
{
class Program
{


static void Main(string [] args)
{
//initialize an instance of this class object to call attributed method to test///
Program o_programObject = new Program();
//call the function via instance///
o_programObject.func_displayInfo_v();
//to keep console application visible to prevent windows auto close after execution///
Console.Read();

}
//create an attribute to tell compiler to not add following entity of code to release version///
[ConditionalAttribute("DEBUG")]
void func_displayInfo_v()
{
//function displays a text for testing///
Console.WriteLine("test text...");
Console.Read();
}
}
}




When above code is compiled, and tried to be executed. If debug compile of visual studio is selected, function is executed. When "release" compile mode is selected in Visual Studio, function is not executed. In order to test, navigate to the project folder after compiling in both mode.

Image of execution result, output of debug compile from folder obj\debug\ConsoleApplication4.exe.

USA citadel

Image of execution result, output of release compile from folder obj\debug\ConsoleApplication4.exe

USA citadel

As it is seen at above images, function is not executed in release version. Attribute is used here for debug informative purpose for developer within developer environment IDE.

When the content of release version IL code is inspected with ildasm disassembler: function is not in assembly.

Image from ildasm diassembler from release compilation of console app.

USA citadel

Image from ildasm diassembler debug compilation of console app.

USA citadel



Data Layers
Area:programming \ Languages \ msil \ \ \
Ref:http://msdn.microsoft.com/en-us/library/78f4aasd.aspx
Loc:articles
Tags: msil
Related
#Updates:
#Blogs:
#Reviews:
#News:


Messages


Feedback:


63
pdf icon Pınned News

AI Citadels

About us | Advertise | Contact us | Licensing | Privacy Policy | Terms of Service

© 2001 AIcitadels. All rights reserved.


Layout: Fixed / Responsive / Old style