reflection on properties or methods

R

raulavi

best regards to you,
what is the best way to get properties' attributes from a class using
reflection.

I only need properties that belong to the current class (not inherited) and
public

thanks
 
R

raulavi

trouble with getting atrib for the properties.

Peter Duniho said:
Do you want to the properties themselves, or their attributes? What are
you having trouble with?


You can pass an appropriate combination of BindingFlags to the
Type.GetProperties() method to specify which properties you really want.

Pete
 
R

refl

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)
 
R

refl

.... Your previous reply said "trouble with getting attrib
for the properties". This post says "trouble:I am getting too many
properties".
the first one did not say how many. the second one did.

I am getting inherited properties. don't want these.
thanks for asking.
 
N

nguyen.panther

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

typeof(myclass).GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance);

Best regards.
 
R

refl

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;

}
}
}


thanks for any help
 
N

nguyen.panther

Hi.

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));
 
R

refl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top