How to Get Property attribute from instance variable
-
public class MyClass { private string _MyProperty = "the property"; [colName="theColumn"] public string MyProperty { get { return _MyProperty; } set { _MyProperty = value; } } public string GetName() { // code to implement } } public class MyClassTest { MyClass c=new MyClass(); // I want to get the attribute in the following way: string colName= c.MyProperty.GetName(); //colName should be theColumn } any kind of help would be appriciated. and it should be in .NET 2.0 framework
-
public class MyClass { private string _MyProperty = "the property"; [colName="theColumn"] public string MyProperty { get { return _MyProperty; } set { _MyProperty = value; } } public string GetName() { // code to implement } } public class MyClassTest { MyClass c=new MyClass(); // I want to get the attribute in the following way: string colName= c.MyProperty.GetName(); //colName should be theColumn } any kind of help would be appriciated. and it should be in .NET 2.0 framework
Why do you want to do this ? The value is going to exist at the class level, you can't change attributes for class instances AFAIK. They are used to specify things for all instances of a class.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Why do you want to do this ? The value is going to exist at the class level, you can't change attributes for class instances AFAIK. They are used to specify things for all instances of a class.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
I want to do this for the following scenarios: say I want to Get an object into database. And I need the column name for WHERE clause e.g // Get employees Get method is like as follows EmpDA.Get(string filterExpression) { //------- the code } Employee emp=DAEmp.GetByID(7); emp.Salary=20000; EmpDA.Get(string.format("{0}>{1}",emp.Salary.GetFieldName,emp.Salary); please look at the following factors. 1) I am getting emp list using where clause as "Salary> 20000" 2) But I want to Get the column name "Salary" using emp.Salary.GetFieldName() thanks for your reply
-
I want to do this for the following scenarios: say I want to Get an object into database. And I need the column name for WHERE clause e.g // Get employees Get method is like as follows EmpDA.Get(string filterExpression) { //------- the code } Employee emp=DAEmp.GetByID(7); emp.Salary=20000; EmpDA.Get(string.format("{0}>{1}",emp.Salary.GetFieldName,emp.Salary); please look at the following factors. 1) I am getting emp list using where clause as "Salary> 20000" 2) But I want to Get the column name "Salary" using emp.Salary.GetFieldName() thanks for your reply
As Graus said, i too dont find its a better solution for this problem! but still, you can get the custom attribute values by the following statement.
colNameAttribute mAttribInstance = (colNameAttribute)c.GetType()
.GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);///remember the array index is 0. you can change it accordingly... ////
The object mAttribInstance will hold the various properties values within your "colName" attribute class. and you can access them by their respective name.. for e.g if "name" is the string used then,
mAttribInstance.Name
will give you the value "theColumn"Have a Happy Coding.....
-
As Graus said, i too dont find its a better solution for this problem! but still, you can get the custom attribute values by the following statement.
colNameAttribute mAttribInstance = (colNameAttribute)c.GetType()
.GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);///remember the array index is 0. you can change it accordingly... ////
The object mAttribInstance will hold the various properties values within your "colName" attribute class. and you can access them by their respective name.. for e.g if "name" is the string used then,
mAttribInstance.Name
will give you the value "theColumn"Have a Happy Coding.....
Thanks King Julien for your reply. I have already used it when interacting with database for insert update fields but now my problem is to get the column name "theColumn" in the following fashion. string colName= emp.Salary.GetColName(); //colName should be "theColumn"; look at the GetColName() after the property Salary. emp.Salary.GetColName(); I am using c# 2.0 framework.
-
Thanks King Julien for your reply. I have already used it when interacting with database for insert update fields but now my problem is to get the column name "theColumn" in the following fashion. string colName= emp.Salary.GetColName(); //colName should be "theColumn"; look at the GetColName() after the property Salary. emp.Salary.GetColName(); I am using c# 2.0 framework.
i see... in that case you may need to create a new class with the function GetColName(); and use it as your return type for property. example:
Class yourClass
{
public string GetColName()
{
colNameAttribute mAttribInstance = (colNameAttribute)this.GetType().GetCustomAttributes(typeof(colNameAttribute),false).GetValue(0);return mAttribInstance.Name;
}
}and u can set the return type of the property salary as yourclass and you will be able to access the GetColName() method!!
Have a Happy Coding.....