I want to use GetProperties to get the properties from the parent class via the child and, despite researching this, have been unsuccessful.
I tried the next without any result:
PropertyInfo[] fields = t.GetProperties();
PropertyInfo[] fields1 = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
PropertyInfo[] propNames = t.BaseType.GetProperties( BindingFlags.Public | BindingFlags.Instance);
Just got it the properties from the child class, but dont get the properties from the parent.
Classes
public class A: B
{
public string a1 { get; set; }
public string a2 { get; set; }
public string a3 { get; set; }
public string a4 { get; set; }
}
public class B
{
public string b1;
}
Using this code I am getting A's properties but not the property in B.
Does this code work? Do I need to configure something in some place?
In your declaration
public class B
{
public string b1;
}
b1 is a field, not a property. You should either
Use GetFields():
FieldInfo[] fields = t.GetFields();
which will get the fields (as expected) - note that the documentation says that
Generally, you should use fields only for variables that have private or protected accessibility.
Make b1 a property, e.g. by adding { get; set; } accessors to it.
Related
According to the documentation, Type.GetProperty(string, BindingFlags) throws AmbiguousMatchException when:
More than one property is found with the specified name and matching
the specified binding constraints
I'm looking for an example type where the GetProperty method would throw because more than one property is found with the same name. I created an inheritance relationship (A : B) where both classes define the same named public property (using the new keyword), with BindingFlags = Public | Instance, but that doesn't throw.
In C#, a class can implement multiple indexers, all of which are called Item.
public class Class1
{
public string this[int firstParameter]
{
get { return ""; }
}
public string this[string firstParameter, int secondParameter]
{
get { return ""; }
}
}
Then you can produce the exception using this:
class Program
{
static void Main()
{
// This will throw AmbiguousMatchException:
typeof(Class1).GetProperty("Item", BindingFlags.Public | BindingFlags.Instance);
}
}
That will produce the AmbiguousMatchException with a single class and the Public and Instance binding flags.
This is an example using BindingFlags.FlattenHierarchy that leads to a name clash between a static and instance property.
public class Program
{
public class A
{
public static string Property { get; set; }
}
public class B : A
{
public string Property { get; set; }
}
public static void Main(string[] args)
{
var type = typeof(B);
var property = type.GetProperty(
"Property",
BindingFlags.Public |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.FlattenHierarchy);
}
}
You can get ambiguous match if you'll play with BindingFlags. For example BindingFlags.IgnoreCase allows you to get this exception despite not having properties with the same name:
class MyClass
{
public string MyProperty {get; set;}
public int Myproperty {get; set;}
}
typeof(MyClass).GetProperty("MyProperty", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase)
Also next set up with BindingFlags.FlattenHierarchy produces the mentioned error:
class MyClass : Base
{
public new string MyProperty { get; set; }
}
class Base
{
public static string MyProperty {get;set;}
}
typeof(MyClass).GetProperty("MyProperty",
BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
I have some configuration stored in the following data structure which uses nested classes for Organization.
public abstract class LogoSpec
{
public abstract byte[] Logo { get; set; }
public class Web
{
public static float Height { get; set; }
public class A4 : Web
{
public static float Left { get; set; }
}
}
}
public class SampleLogo : LogoSpec
{
public override byte[] Logo { get; set; }
}
I can easily use it when I exactly know what value to use at design time
// Setting values
SampleLogo.Web.A4.Height = 10.25f;
How can I write a function that retrieves this value at runtime?
float GetValue(string logoName = "SampleLogo", string layout = "Web", string paperSize = "A4", string property = "Height");
The way to get that property is actually very straight forward, but you have to provide all necessary BindingFlags:
PropertyInfo p = typeof(SampleLogo.Web.A4).GetProperty("Height",
BindingFlags.Static |
BindingFlags.FlattenHierarchy |
BindingFlags.Public);
FlattenHierarchy is needed to also get properties of base classes.
Now you can use this PropertyInfo to get and set the value:
p.SetValue(null, 14f);
float height = (float)p.GetValue(null);
Update: The complete method could look like this:
public float GetValue(string logoName = "LogoSpec", string layout = "Web", string paperSize = "A4", string property = "Height")
{
Type logoType = Type.GetType(logoName);
Type layoutType = logoType?.GetNestedType(layout);
Type paperType = layoutType?.GetNestedType(paperSize);
PropertyInfo pi = paperType?.GetProperty("Height", BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
return (float?)pi?.GetValue(null) ?? 0f;
}
But note that instead for "LogoSpec" you would need to use an AssemblyQualifiedName or at least qualify the type name with its namespace.
I tried to use the code PropertyInfo[] but the code is not getting any attribute. Because the attribute inside class1 is just a reference variable
public readonly subClass1 sClass1;
public readonly subClass2 sClass2;
public readonly subClass3 sClass3;
public class1()//constructor
{
sClass1= new subClass1();
sbClass2= new subClass2();
sClass3= new subClass3();
}
My problem is, i can't access those 3 classes just by using PropertyInfo[]
but my i can access it using
Type type = typeof(class1);
FieldInfo[] fields = type.GetFields();
var i = 0;
foreach (var field in fields)
{
var f = field.GetType().GetFields();
i++;
}
But this code is not working as i want to work, they can get the class but i can't get the property of every subClass
What i want is something like this
var f = class1.sClass1;
my variable f will now hold the every property of a class.
I'm sorry if i can't explain it well. If you want to ask something, just comment below
by the way this is the inside code of every subClass
public string X{ get; set; }
public string Y{ get; set; }
public string Width { get; set; }
public string Length { get; set; }
You will have to do it recursively
void GetFields(Type type)
{
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance
| BindingFlags.Public | BindingFlags.NonPublic)) {
Console.WriteLine("{0} {1}", fieldInfo.FieldType.Name, fieldInfo.Name);
if (fieldInfo.FieldType.IsClass)
GetFields(fieldInfo.FieldType);
}
You have to use field.FieldType instead of using field.GetType()
I have a little problem, below my code:
public class A
{
public string ObjectA { get; set; }
}
public void Run()
{
A a = new A();
a.ObjectA = "Test number 1";
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
PropertyInfo myPropertyInfo = a.GetType().GetProperty("ObjectA", flags);
object myNewObject = myPropertyInfo.GetValue(a);// Here should be reference
a.ObjectA = "Test number 2";//After this line value myNewObject continued "Test number 1"
}
So my value myNewObject must be in output "Test number 2". Is there any way? It is this at all possible?
Wrong!
You're getting the string rather than the instance of A using reflection.
Changing A.ObjectA doesn't change the string reference. Actually, you're setting a different string to the backing string class field by the ObjectA property...
Auto-properties are syntactic sugar to avoid explicitly declaring class fields to properties which perform nothing when getting or setting their values:
// For example:
public string Text { get; set; }
// is...
private string _text;
public string Text { get { return _text; } set { _text = value; } }
Now turn your code into regular one (no reflection):
A a = new A();
a.ObjectA = "hello world";
object myNewObject = a.ObjectA;
// Do you think now that myNewObject should change...? :)
a.ObjectA = "goodbye";
Is there any way? It is this at all possible?
No.
Maybe you can simulate this behavior using a Func<T>...
Func<object> myNewObjectGetter = () => myPropertyInfo.GetValue(a);
Now, whenever you call myNewObjectGetter() you're going to get the most fresh value of the whole property. BTW, this still doesn't address the impossible!
Is there any way?
No. You can't put a reference to a property into an object variable. Such a variable can only hold a normal value, such as the string you put into it.
That answers the question as asked. You can clarify what you want to achieve and maybe we can suggest a better way.
Probably there is no solution but I show some code
public class MyRows
{
public string Key { get; set; }
public int Id { get; set; }
public object Val { get; set; }
}
public abstract class BasicDTO
{
public int? Id { get; private set; }
public PropertyInfo[] PropertyDTO;
protected Type myType;
public BasicDTO()
{
Load();
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
PropertyDTO = myType.GetProperties(flags);
}
}
public class CustomerDTO : BasicDTO
{
public string Surname { get; set; }
public string Phone { get; set; }
public CustomerDTO() { }
protected override void Load()
{
myType = typeof(CustomerDTO);
}
}
Now my basic method
public void Run(BasicDTO dto)
{
PropertyInfo pi = dto.PropertyDTO.Where(x => x.Name == "Surname").SingleOrDefault();
MyRows mr = new MyRows();
mr.Val = pi.GetValue(dto);//Here I need reference
}
When I change CustomerDTO.Surname my value mr.Val it must also be changed.
As I wrote above, it is probably impossible, but maybe anybody have a idea.
BTW: Value mr.Val I use only for binding (WPF). So maybe you have any other suggestions, how solve problem. I will be grateful for your help
I am trying to retrieve the public properties of an object but it is returning nothing. Can you tell me what I'm doing wrong.
public class AdHocCallReportViewModel : ReportViewModel
{
public string OperatorForCustEquipID { get; set; }
public string OperatorForPriorityID { get; set; }
public string OperatorForCallTypeID { get; set; }
public string OperatorForStatusID { get; set; }
}
public UpdateReportParameters(AdHocCallReportViewModel rvm)
{
var type = rvm.GetType();
foreach (var f in type.GetFields().Where(f => f.IsPublic))
{
Console.WriteLine(f.Name);
Console.WriteLine(f.GetValue(rvm).ToString());
}
}
When stepping through the code, it skips over the foreach loop because GetFields returns zero items.
You haven't got public fields. They are properties. So try type.GetProperties() instead.
You are trying to get fields, you should try to call GetProperties()
Pass BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public to get all instance fields.
On second thought, I'm seeing that you are explicitly filtering for public fields. The class does not have any public fields. The fields that are automatically generated by the compiler as the backing store for the properties are private.