What are the purposes of using attributes?
I know that Attributes are usefull in reflecting, is there any usecase else?
with thanks
I know that Attributes are usefull in reflecting, is there any usecase else?
with thanks
Any time you want to attach extra information to an assembly, type or type member. Reflection is just the way to read those attributes at runtime. The actual uses are many. Just look at how the framework are using them for serialization, security, designer support etc.
Thanks dear.
Look at hear:
using System;
using System.Collections.Generic;
using System.Text;
using Atinegar.Library.ObjectModel;
namespace Atinegar.Library.ObjectModel
{
[Creator("Mohammad"), Construct(ConstructProgressive.UnderTesting)]
publicstaticclassAttributeHelper
{
publicstaticAttribute GetAttribute(Type attributeType, Type objectType){
object[] attributes = objectType.GetCustomAttributes(attributeType, false);if (attributes == null || attributes.Length == 0) returnnull;foreach (Attribute attribute in attributes) if (attribute.GetType() == attributeType) return attribute;returnnull;}
}
publicenumConstructProgressive{
None,
ForTest,
Obsoleted,
NotStarted,
InProgress,
ErrorOccured,
Completed,
UnderTesting
}
[
Creator("Mohammad"), Construct(ConstructProgressive.InProgress), AttributeUsage(AttributeTargets.All)]publicclassConstructAttribute : System.Attribute{
#region ctors
public ConstructAttribute(ConstructProgressive progressive)
{
this.Progressive = progressive;
}
public ConstructAttribute()
{
this.Progressive = ConstructProgressive.None;
}
#endregion
#region Progressive
privateConstructProgressive progressive;
publicConstructProgressive Progressive
{
get { return progressive; }
privateset { progressive = value; }
}
#endregion
#region GetAttribute
publicstaticConstructAttribute GetAttribute(Type type)
{
ConstructAttribute result = (ConstructAttribute)AttributeHelper.GetAttribute(typeof(ConstructAttribute), type);
if (result == null) result = newConstructAttribute();
return result;
}
#endregion
}
[
Creator("Mohammad"), Construct(ConstructProgressive.Completed)][
AttributeUsage(AttributeTargets.All)]publicclassCreatorAttribute : System.Attribute{
privatestring name;publicvirtualstring Name{
get { return name; }privateset { this.name = value; }}
public CreatorAttribute(string name){
this.name = name;}
private CreatorAttribute(){
this.Name = "Unknown";}
#region GetAttribute
publicstaticCreatorAttribute GetAttribute(Type type)
{
CreatorAttribute result = (CreatorAttribute)AttributeHelper.GetAttribute(typeof(CreatorAttribute), type);
if (result == null) result = newCreatorAttribute();
return result;
}
#endregion
}
}Or post if you need more help! Thanks