simple:
how do you get all attributes that belong to a public property.
if a class have few properties I want to get each property with its
attributes.
the filter should be for properties with public and not inherited from any
other class.
(trouble:I am getting too many properties. I just one as above...thanks)
Hi.
I think it 's simple to you. See sample below:
==> I have tree class.
public class baseclass{
private int _ID;
private string _Name;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
public class parentclass : baseclass
{
private int _parentclassID;
private int _parentclassName;
public int ParentclassID
{
get { return _parentclassID; }
set { _parentclassID = value; }
}
public int ParentclassName
{
get { return _parentclassName; }
set { _parentclassName = value; }
}
}
public class myclass: parentclass
{
private int _myclassID;
private string _myclassName;
public int MyclassID
{
get { return _myclassID; }
set { _myclassID = value; }
}
public string MyclassName
{
get { return _myclassName; }
set { _myclassName = value; }
}
}
==> here the code that i want to get public properties that belong to
myclass only despare that it inherited from many class (in this case
is 2 class). simple using bindingflag to get what you want like this
nguyen.panther or anyone:
when trying to CAST the object to my custom attr i got an error
""... cannot cast.."
SOSIsTemplateAttribute a = attr as
SOSIsTemplateAttribute;
even when i check doing the foolowing code:
....some code omitted..
PropertyInfo[] propQ = eaClass.GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in propQ)
{
object[] attrQ = prop.GetCustomAttributes(false); //not inherited
foreach (object attr in attrQ)
{
if (attr.GetType().FullName ==
"TestClassForTemplates.SOSIsTemplateAttribute")
// NEXT LINE CAUSES ERROR
{
TestClassForTemplates.SOSIsTemplateAttribute a = attr as
SOSIsTemplateAttribute;
I have test your code with the following code, that 's ok. May be you
must have a look at the SOSIsTemplateAttribute class.
Here 's the code of information class and attribute class (simple).
namespace WindowsApplication1
{
public class baseclass
{
private int _ID;
private string _Name;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
public class parentclass : baseclass
{
private int _parentclassID;
private int _parentclassName;
[SOSIsTemplate("ParentclassID")]
public virtual int ParentclassID
{
get { return _parentclassID; }
set { _parentclassID = value; }
}
public int ParentclassName
{
get { return _parentclassName; }
set { _parentclassName = value; }
}
}
public class myclass : parentclass
{
private int _myclassID;
private string _myclassName;
[SOSIsTemplate("MyclassID")]
public int MyclassID
{
get { return _myclassID; }
set { _myclassID = value; }
}
[SOSIsTemplate("MyclassName")]
public string MyclassName
{
get { return _myclassName; }
set { _myclassName = value; }
}
public override int ParentclassID
{
get { return base.ParentclassID; }
set { base.ParentclassID = value; }
}
}
}
namespace TestClassForTemplates
{
public class SOSIsTemplateAttribute : Attribute
{
public SOSIsTemplateAttribute(string name)
{
id = name.GetHashCode();
this.name = name;
}
private int id;
private string name;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
}
Here 's the function get attribute for class
public List<SOSIsTemplateAttribute> getattributes(Type eaClass)
{
List<SOSIsTemplateAttribute> arr = new
List<SOSIsTemplateAttribute>();
PropertyInfo[] propQ = eaClass.GetProperties
(BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.Instance);
foreach (PropertyInfo prop in propQ)
{
object[] attrQ = prop.GetCustomAttributes(false); //
not inherited
foreach (object attr in attrQ)
{
if (attr.GetType().FullName ==
"TestClassForTemplates.SOSIsTemplateAttribute")
{
SOSIsTemplateAttribute a = attr as
SOSIsTemplateAttribute;
if(a!=null)
arr.Add(a);
}
}
}
return arr;
}
You can also use a datagridview to view your attributes like
DataGridView4ParentClass.DataSource = getattributes(typeof
(parentclass));
DataGridView4MyClass.DataSource = getattributes(typeof(myclass));
thanks , (I thought I was doing something wrong).
you are right, went home tested the code in other PC and it works.
I will chase what's wrong. thanks for the tip "use a datagridview to view
your attributes " i will try that one too.