say I have the attribute:
public class Column_Attribute : Attribute
{
public string DbType { get; set; }
public bool IsPrimaryKey { get; set; }
}
then I can apply that attribute to a property as:
[Column_Attribute(DbType = "Integer", IsPrimaryKey = true)]
public int Id { get; set; }
Now how can I get information about the property Id from the attribute class. In other words I want to do something like:
public class Column_Attribute : Attribute
{
// constructor
public Column_Attribute(){
// if the property has the name Id do something...
// OR
// if this is an attribute of a property do something
// if this is an attribute of a field do something else
// If this attribute is targeting a property that is a string do something
}
public string DbType { get; set; }
public bool IsPrimaryKey { get; set; }
}
I actually need to know if the attribute is being applied to a property that is a string.
I know how to do that with reflection but I want to do that inside the attribute class. Is that possible. Hope I am explaining myself correctly
You cannot do that without reflection because the code in the constructor will not be executed until you call
GetCustomAttributes(), which is a part of reflection.see http://msdn.microsoft.com/en-us/library/z919e8tw(v=vs.80).aspx
If you want your attribute class to contain the processing code, you could create a method receiving the property name. The property name will be available at the time of calling GetCustomAttributes().