Dynamically identifying properties in C# - c#

Is there a way to dynamically identify design time properties in C#? For example:
class MyClass
{
public string MyProperty1 { get; set; }
}
And then reference it something like this:
string myVar = "MyProperty1";
MyClass.myVar = "test";

If you want to set the value of a property at runtime and the name of the property is only known at runtime you need to use Reflection. Here's an example:
public class MyClass
{
public string MyProperty1 { get; set; }
}
class Program
{
static void Main()
{
// You need an instance of a class
// before being able to set property values
var myClass = new MyClass();
string propertyName = "MyProperty1";
// obtain the corresponding property info given a property name
var propertyInfo = myClass.GetType().GetProperty(propertyName);
// Before trying to set the value ensure that a property with the
// given name exists by checking for null
if (propertyInfo != null)
{
propertyInfo.SetValue(myClass, "test", null);
// At this point you've set the value of the MyProperty1 to test
// on the myClass instance
Console.WriteLine(myClass.MyProperty1);
}
}
}

how about simply implementing an indexer on your class
public class MyClass
{
public string MyProperty1 { get; set; }
public object this[string propName]
{
get
{
return GetType().GetProperty(propName).GetValue(this, null);
}
set
{
GetType().GetProperty(propName).SetValue(this, value, null);
}
}
}
and then you can do something very similar
var myClass = new MyClass();
string myVar = "MyProperty1";
myClass[myVar] = "test";

Yes, of course you can. You need to get a FieldInfo object relating to the property that you want to set.
var field = typeof(MyClass).GetField("MyProperty1");
then from that field info object, you can set the value of any instance of that class.
field.SetValue(myinstanceofmyclass, "test");
See MSDN: FieldInfo for other fun stuff you can do with reflection.

Related

Set Object Property of Object Property by Reflection

I used this SO Question to retrieve a property of an object using reflection. The property I retrieved is another object that has a property called Value that I need to access. All of the potential objects that I retrieve using reflection derive from the same class EntityField and therefore all have a Value property. I saw this SO question that hinted at how I might be able to access the Value property, but I couldn't quite put together the correct code. How can I access the Value property on an object retrieved by reflection?
My Attempts
var parent = entity.GetType().GetProperty("Property");
parent.GetType().GetProperty("Value").SetValue(parent, newValue); // parent.GetType() is null
(parent as EntityField<T>).Value = newValue; // Not sure how to dynamically set T since it could be any system type
Main (Original Code)
private static void SetValues(JObject obj, EntityBase entity)
{
// entity.GetType().GetProperty("Property") returns an EntityField Object
// I need to set EntityField.Value = obj["Value"]
// Current code sets EntityField = obj["Value"] which throws an error
entity.GetType().GetProperty("Property").SetValue(entity, obj["Value"], null);
}
EntityField
public class EntityField<T> : EntityFieldBase
{
private Field _Field;
private T _Value;
public EntityField(Field field, T value){
this._Field = field;
this._Value = value;
}
public Field Field
{
get
{
return this._Field;
}
set
{
if (this._Field != value)
{
this._Field = value;
}
}
}
public T Value
{
get
{
return this._Value;
}
set
{
if (!EqualityComparer<T>.Default.Equals(this._Value, value))
{
this._Value = value;
this._IsDirty = true;
}
}
}
}
Try this:
entity.GetType().GetProperty("Value").SetValue(entity, obj["Value"], null);
You need to specify the name of the property in the GetProperty() method. I suspect there was no such property called 'Property' :)
Edit: After reading your comments try
entity.Property.GetType().GetProperty("Value").SetValue(entity, obj["Value"], null);
Tried the following in LinqPad and it worked...
class TestChild<T>
{
public T ChildProperty { get; set; }
}
class TestParent<T>
{
public TestChild<T> ParentProperty { get; set; }
}
void Main()
{
var instance = new TestParent<string>
{
ParentProperty = new TestChild<string>()
};
instance.GetType()
.GetProperty("ParentProperty")
.GetValue(instance)
.GetType()
.GetProperty("ChildProperty")
.SetValue(instance.ParentProperty, "Value");
Console.WriteLine(instance.ParentProperty.ChildProperty);
}

Determine if a generic class type needs to be instantiated [duplicate]

This question already has answers here:
Best way to test if a generic type is a string? (C#)
(5 answers)
Closed 5 years ago.
I have a class Data<T> which I use to store data received on a communication link. The Data<T> class contains a Value property of type T and a Timestamp property of type DateTime.
Is it possible to determine in the class constructor that the requested type's default value is null, and in that case instantiate it?
I've looked at the new() class constraint. But then I'm having trouble using the class with a string type, as I'm getting errors about string not having a parameterless constructor.
Here's an example causing a NullReferenceException on dataValue4.Value, since the Value property of type Foo is not instantiated.
public class Data<T> {
public Data() {
_timestamp = DateTime.Now;
}
private T _value;
public T Value {
get { return _value; }
set {
_value = value;
_timestamp = DateTime.Now;
}
}
private DateTime _timestamp;
public DateTime Timestamp {
get { return _timestamp; }
}
}
public class Foo {
public int Value1 { get; set; }
public bool Value2 { get; set; }
}
public class Program {
public static void Main() {
var dataValue1 = new Data<int>();
var dataValue2 = new Data<bool>();
var dataValue3 = new Data<string>();
var dataValue4 = new Data<Foo>();
//dataValue4.Value = new Foo();
Console.WriteLine(dataValue1.Value); // Output is "0"
Console.WriteLine(dataValue2.Value); // Output is "False"
Console.WriteLine(dataValue3.Value); // Output is ""
Console.WriteLine(dataValue4.Value.Value1); // Output is "0"
Console.WriteLine(dataValue4.Value.Value2); // Output is "False"
}
}
You can create a instance with Activator.
var type = typeof(T);
var value = Activator.CreateInstance(type) as T;
This will create a instance with the default value of the type.

Unable to Set and Get the values in C#

I am trying to use setter and getter. When i debug, the value gets set but when i try to retrieve, it gets null value.
Class1.cs
private string setMAX;
public string SETMax
{
get
{
return setMAX;
}
set
{
setMAX = value;
}
}
private string value1;
public string MaxValue
{
get
{
return value1;
}
set
{
value1= value;
}
}
Class2.cs
Class1.SETMax = Class1.value1; //This gets set
Class3.cs
//When i debug, first Class1.cs and Class2.cs completes, then it comes in Class3.cs
string max = Class1.SETMax; //I GET NULL here.
I dont know where I am wrong here.Can anyone please explain me ?
You are referencing File1 as an instance. You are probably referencing different instances. You probably want static properties.
private static string setMAX;
public static string SETMax
{
get
{
return setMAX;
}
set
{
setMAX = value;
}
}
I think you have a few things mixed up so lets start from the beginning
Class1.SETMax = Class1.value1;
// for a start you are assigning a
// private variable to a public one
// via the Class definition I'm not even sure how that compiles.
Have a look here see if this makes sense to you
// This is a Class definition
public class Class1 {
public string SETMax {get; set;}
public int MaxValue {get; set;}
}
// This is your application
public class MyApp{
// this is a private field where you will assign an instance of Class1
private Class1 class1Instance ;
public MyApp(){
//assign the instance in the constructor
class1Instance = new Class1();
}
public void Run {
// now for some fun
class1Instance.SETMax = "Hello";
Console.WriteLine(class1Instance.SETMax); // outputs "Hello"
var localInstance = new Class1();
localInstance.SETMax = class1Instance.SETMax;
Console.WriteLine(localInstance.SETMax); // outputs "Hello"
}
}

Is there a wa to call a method immedietely after object instantiation?

public class Foo
{
public Foo(){ }
//One of many properties
//set up in the same way
private String _name;
public String Name
{
get { return _name; }
set {
_name = value;
//code that is important to run
//only after the objects initial creation
}
}
private int _id;
public int ID
{
get { return _id; }
set {
_id = value;
//code that is important to run
//only after the objects initial creation
}
}
public void Win()
{
//clean up method that wouldn't be needed
//if I used optional parameters because
//i would be able to set _name (and all the
//other private properties directly without
//using the public Set
}
}
How do I call a method automatically after this kind of object creation in c#
Foo ko = new Foo() {
ID = 4,
Name = "Chair"
};
ko.Win(); // <-- Want this to be called automatically inside the class
There is no method that automatically called after some random set of properties is set (Which is what initialization is translated to...)
var foo = new Foo { Name = "bar" };
Is actually shortcut to:
var foo = new Foo();
foo.Name = "bar";
When written in second form one would not expect any magical method to be called after foo.Name assignment.
You options:
if you have some information that need to be set on property change - just make it a property and write code in set part of it.
if you must have particular set of properties configured before object is considered "created" constructor arguments is one reasonable way to enforce it.
you can also implement builder pattern that allow you to delay final construction (or use some other factory method that forces setting parameters before final object creation.
Sample of code with builder pattern:
var foo = new FooBuilder { Name = "bar" }
.Build();
add the Win() to the constructor. Call/put inside the constructor.
public Foo(string value, string value2) {
Value = value;
Value2 = valu2e;
Win();
}
This is the constructor. Set it manually.
Well if you are always setting ID and the Name how about this?
private string _Name;
public string Name
{
get { return _Name; }
set {
_Name = value;
this.Win();
}
}
Win function will always called after you set a value on the name or you can do this for ID that's your choice!
Not the most scalable solution but why not try this:
public class Foo
{
public int ID { get; set; }
public String Name { get; set; }
public Foo()
{
}
public Foo( int id )
{
// Win()
ID = id;
// Win()
}
Public Foo( string name )
{
// Win()
Name = name;
// Win()
}
public Foo( int id, string name )
{
// Win()
ID = id;
Name = name;
// Win()
}
public void Win()
{
//Do Stuff that would go into the constructor
//if I wanted to use optional parameters
//but I don't
}
}
You can call Win before or after setting the properties.

How do I reinitialize or reset the properties of a class?

I've created a class with properties that have default values. At some point in the object's lifetime, I'd like to "reset" the object's properties back to what they were when the object was instantiated. For example, let's say this was the class:
public class Truck {
public string Name = "Super Truck";
public int Tires = 4;
public Truck() { }
public void ResetTruck() {
// Do something here to "reset" the object
}
}
Then at some point, after the Name and Tires properties have been changed, the ResetTruck() method could be called and the properties would be reset back to "Super Truck" and 4, respectively.
What's the best way to reset the properties back to their initial hard-coded defaults?
You can have the initialization in a method instead of inlining with the declaration. Then have the constructor and reset method call the initialization method:
public class Truck {
public string Name;
public int Tires;
public Truck() {
Init();
}
public void ResetTruck() {
Init();
}
private void Init() {
Name = "Super Truck";
Tires = 4;
}
}
Another way is not to have a reset method at all. Just create a new instance.
Reflection is your friend. You could create a helper method to use Activator.CreateInstance() to set the default value of Value types and 'null' for reference types, but why bother when setting null on a PropertyInfo's SetValue will do the same.
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
for (int i = 0; i < properties.Length; ++i)
properties[i].SetValue(this, null); //trick that actually defaults value types too.
To extend this for your purpose, have private members:
//key - property name, value - what you want to assign
Dictionary<string, object> _propertyValues= new Dictionary<string, object>();
List<string> _ignorePropertiesToReset = new List<string>(){"foo", "bar"};
Set the values in your constructor:
public Truck() {
PropertyInfo[] properties = type.GetProperties();
//exclude properties you don't want to reset, put the rest in the dictionary
for (int i = 0; i < properties.Length; ++i){
if (!_ignorePropertiesToReset.Contains(properties[i].Name))
_propertyValues.Add(properties[i].Name, properties[i].GetValue(this));
}
}
Reset them later:
public void Reset() {
PropertyInfo[] properties = type.GetProperties();
for (int i = 0; i < properties.Length; ++i){
//if dictionary has property name, use it to set the property
properties[i].SetValue(this, _propertyValues.ContainsKey(properties[i].Name) ? _propertyValues[properties[i].Name] : null);
}
}
Unless creating the object is really expensive (and Reset isn't for some reason). I see no reason to implement a special reset method. Why don't you just create a new instance with a usable default state.
What is the purpose of reusing the instance?
If you did your initialization in a Reset method you can be good to go:
public class Truck {
public string Name;
public int Tires;
public Truck() {
ResetTruck();
}
public void ResetTruck() {
Name = "Super Truck";
Tires = 4;
}
}
Focusing of separation of concerns (like Brian mentioned in the comments), another alternative would be to add a TruckProperties type (you could even add your default values to its constructor):
public class TruckProperties
{
public string Name
{
get;
set;
}
public int Tires
{
get;
set;
}
public TruckProperties()
{
this.Name = "Super Truck";
this.Tires = 4;
}
public TruckProperties(string name, int tires)
{
this.Name = name;
this.Tires = tires;
}
}
Inside your Truck class, all you would do is manage an instance of the TruckProperties type, and let it do its reset.
public class Truck
{
private TruckProperties properties = new TruckProperties();
public Truck()
{
}
public string Name
{
get
{
return this.properties.Name;
}
set
{
this.properties.Name = value;
}
}
public int Tires
{
get
{
return this.properties.Tires;
}
set
{
this.properties.Tires = value;
}
}
public void ResetTruck()
{
this.properties = new TruckProperties();
}
}
This certainly may be a lot of (unwanted) overhead for such a simple class, but in a bigger/more complex project it could be advantageous.
That's the thing about "best" practices... a lot of times, there's no silver bullet, but only recommendations you must take with skepticism and your best judgement as to what applies to you in a particular case.
I solved a similar problem with reflection. You can use source.GetType().GetProperties() to get a list of all properties which belong to the object.
Although, this is not always a complete solution. If your object implements several interfaces, you will also get all those properties with your reflection call.
So I wrote this simple function which gives us more control of which properties we are interested in resetting.
public static void ClearProperties(object source, List<Type> InterfaceList = null, Type SearchType = null)
{
// Set Interfaces[] array size accordingly. (Will be size of our passed InterfaceList, or 1 if InterfaceList is not passed.)
Type[] Interfaces = new Type[InterfaceList == null ? 1 : InterfaceList.Count];
// If our InterfaceList was not set, get all public properties.
if (InterfaceList == null)
Interfaces[0] = source.GetType();
else // Otherwise, get only the public properties from our passed InterfaceList
for (int i = 0; i < InterfaceList.Count; i++)
Interfaces[i] = source.GetType().GetInterface(InterfaceList[i].Name);
IEnumerable<PropertyInfo> propertyList = Enumerable.Empty<PropertyInfo>();
foreach (Type face in Interfaces)
{
if (face != null)
{
// If our SearchType is null, just get all properties that are not already empty
if (SearchType == null)
propertyList = face.GetProperties().Where(prop => prop != null);
else // Otherwise, get all properties that match our SearchType
propertyList = face.GetProperties().Where(prop => prop.PropertyType == SearchType);
// Reset each property
foreach (var property in propertyList)
{
if (property.CanRead && property.CanWrite)
property.SetValue(source, null, new object[] { });
}
}
else
{
// Throw an error or a warning, depends how strict you want to be I guess.
Debug.Log("Warning: Passed interface does not belong to object.");
//throw new Exception("Warning: Passed interface does not belong to object.");
}
}
}
And it's use:
// Clears all properties in object
ClearProperties(Obj);
// Clears all properties in object from MyInterface1 & MyInterface2
ClearProperties(Obj, new List<Type>(){ typeof(MyInterface1), typeof(MyInterface2)});
// Clears all integer properties in object from MyInterface1 & MyInterface2
ClearProperties(Obj, new List<Type>(){ typeof(MyInterface1), typeof(MyInterface2)}, typeof(int));
// Clears all integer properties in object
ClearProperties(Obj,null,typeof(int));
You'd probably need to save the values off in private fields, so that they can be restored later. Maybe something like this:
public class Truck
{
private static const string defaultName = "Super Truck";
private static const int defaultTires = 4;
// Use properties for public members (not public fields)
public string Name { get; set; }
public int Tires { get; set; }
public Truck()
{
Name = defaultName;
Tires = defaultTires;
}
public void ResetTruck()
{
Name = defaultName;
Tires = defaultTires;
}
}
You're essentially looking for the State Design Pattern
If you want a specific past "state" of your object you can create a particular save point to return every time you want. This also let you have a diferent state to backup for everey instance that you create. If you class has many properties who are in constant change, this could be your solution.
public class Truck
{
private string _Name = "Super truck";
private int _Tires = 4;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public int Tires
{
get { return _Tires; }
set { _Tires = value; }
}
private Truck SavePoint;
public static Truck CreateWithSavePoint(string Name, int Tires)
{
Truck obj = new Truck();
obj.Name = Name;
obj.Tires = Tires;
obj.Save();
return obj;
}
public Truck() { }
public void Save()
{
SavePoint = (Truck)this.MemberwiseClone();
}
public void ResetTruck()
{
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
for (int i = 0; i < properties.Count(); ++i)
properties[i].SetValue(this, properties[i].GetValue(SavePoint));
}
}
If you aren't using a Code Generator or a Designer that would conflict, another option is to go through C#'s TypeDescriptor stuff, which is similar to Reflection, but meant to add more meta information to a class than Reflection could.
using System.ComponentModel;
public class Truck {
// You can use the DefaultValue Attribute for simple primitive properites
[DefaultValue("Super Truck")]
public string Name { get; set; } = "Super Truck";
// You can use a Reset[PropertyName]() method for more complex properties
public int Tires { get; set; } = 4;
public void ResetTires() => Tires = 4;
public Truck() { }
public void ResetTruck() {
// Iterates through each property and tries to reset it
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(GetType())) {
if (prop.CanResetValue(this)) prop.ResetValue(this);
}
}
}
Note that ResetValue will also reset to a shadowed property if one exists. The priority of which option is selected is explained in the docs:
This method determines the value to reset the property to in the following order of precedence:
There is a shadowed property for this property.
There is a DefaultValueAttribute for this property.
There is a "ResetMyProperty" method that you have implemented, where "MyProperty" is the name of the property you pass to it.
You may represent an object state as a struct or record struct and then set the state to the default value in the Reset method like this:
public class Truck {
record struct State(string Name, int Tires);
private static readonly State _defaultState = new("Super Truck", 4);
private State _state = _defaultState;
public string Name => _state.Name;
public int Tires => _state.Tires;
public Truck() {}
public void ResetTruck() => _state = _defaultState;
}
It is probably the fastest way as well.
Also, a record struct will give you the trivial implementations of the ToString, Equals, GetHashCode.

Categories