I am using reflection to assign properties for controls as the control properties are stored in the database.And I am facing issues with following situation.
I have raddateinput control and it is having a property DateInput.DataFormat and when I tried get the propertyinfo for the using following code it returns as null.
ctrl.GetType().GetProperty(propertyName.Split('.').FirstOrDefault(), BindingFlags.Public | BindingFlags.Instance)
.GetType().GetProperty(propertyName.Split('.').LastOrDefault())
ctrl is a Control. propertyName is DateInput.DataFormat
I achieved the solution with the following code.
Private void SetProperty(Object ctrl, string propertyName, string value)
{
string name = propertyName.Split('.').First();
PropertyInfo property = ctrl.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (name != propertyName)
{
ctrl = property.GetValue(ctrl, null);
SetProperty(ctrl, propertyName.Replace(string.Concat(name, "."), string.Empty), value);
return;
}
TypeConverter converter = TypeDescriptor.GetConverter(property.PropertyType);
if (converter != null && converter.CanConvertFrom(typeof(String)))
property.SetValue(ctrl, converter.ConvertFrom(value), null);
}
Related
I have a LookUpEdit control and I need set property value to NullText with reflection, but I'm getting the TargetException:
private static void SetObjectProperty(string propiedad, string valor, object obj)
{
if (obj.GetType() == typeof(LookUpEdit))
{
string[] vv = propiedad.Split('.');
string prop = vv[0];
string propType = vv[1];
var p = obj.GetType().GetProperty(prop, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
PropertyInfo propertyInfo = p.PropertyType.GetProperty(propType);
if (propertyInfo != null)
{
propertyInfo.SetValue(obj, valor, null);
}
}
}
I only get the exception with LookUpEdit control.
"propiedad" is a string contains "Properties.NullText" so this is why I'm doing a split
You should apply operations with nested properties to corresponding nested objects:
static void SetObjectProperty(object obj, string propertyPath, object value) {
if(obj != null && obj.GetType() == typeof(LookUpEdit)) {
string[] parts = propertyPath.Split('.');
var rootInfo = typeof(LookUpEdit).GetProperty(parts[0],
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
object root = rootInfo.GetValue(obj); // obtaining a root
var nestedInfo = rootInfo.PropertyType.GetProperty(parts[1]);
if(nestedInfo != null)
nestedInfo.SetValue(root, value, null); // using root object
}
}
PS. Why you're using this ugly way of modifying object properties?
I'm trying to set a property of an object in a class, but the error says Object does not match target type.
FieldInfo dControl = window.GetType().GetField("dControl", BindingFlags.NonPublic | BindingFlags.Instance);
if (dControl == null) { Debug.Log ("dControl is null"); return;}
Type typeDC = dControl.FieldType;
PropertyInfo inPreviewMode = typeDC.GetProperty("InPreviewMode", BindingFlags.Public | BindingFlags.Instance);
if (inPreviewMode == null) { Debug.Log ("dControl.InPreviewMode is null"); return;}
bool value = false;
inPreviewMode.SetValue(dControl, value, null);
This is the property I'm trying to access:
public class DControl : TimeArea
{
public bool InPreviewMode
{
get
{
return dState.IsInPreviewMode;
}
set
{
if (cutscene != null)
{
...
}
}
dState.IsInPreviewMode = value;
}
...
}
Help is appreciated.
The first parameter of SetValue is the instance for which to set the value on. ie, it is expecting an instance of DControl - your code passes it an instance of FieldInfo.
So you might have to get that instance via reflection:
DControl ctrl = (DControl)dControl.GetValue(window);
And then pass that to the set value
inPreviewMode.SetValue(ctrl, value, null);
I have following piece of code:
ClassName class = new ClassName();
var getValue = GetPrivateProperty<BaseClass>(class, "BoolProperty");
public static T GetPrivateProperty<T>(object obj, string name)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
PropertyInfo field = typeof(T).GetProperty(name, flags);
return (T)field.GetValue(obj, null);
}
Now when I get an InvalidCastException in the return statement that he can not convert the object with type System.Boolean to the type ClassName.
BaseClass has the property. ClassName inherits from BaseClass. In have to access all properties from the "ClassName" Class. Since this property is private, I have to access it directly over the BaseClass. This works but I crashes because the property has the return value boolean.
Thanks!
You got the property of type T and the return value should also be of type T? I don't believe that.
Maybe this will help:
var getValue = GetPrivateProperty<bool>(class, "BoolProperty");
public static T GetPrivateProperty<T>(object obj, string name)
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
PropertyInfo field = null;
var objType = obj.GetType();
while (objType != null && field == null)
{
field = objType.GetProperty(name, flags);
objType = objType.BaseType;
}
return (T)field.GetValue(obj, null);
}
Please see the changes of <BaseClass> to <bool> and typeof(T).GetProperty to obj.GetType().GetProperty.
i have used the following code to change the current value for the current field value as
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, connectionString);
but my query is to get current value of connectionstringfied...
i tried the below code as
getvalue(obj ss);
waiting for your valuable esponses
it throws me null values
If connectionStringField has found the field (i.e. it is in the base type and is called "_sqlConnectionString", then it should just be:
string connectionString = (string)connectionStringField.GetValue(this);
?
However, using reflection to talk to non-public fields is... unusual.
public static string GetPropertyValue<T>(this T obj, string parameterName)
{
PropertyInfo[] property = null;
Type typ = obj.GetType();
if (listPropertyInfo.ContainsKey(typ.Name))
{
property = listPropertyInfo[typ.Name];
}
else
{
property = typ.GetProperties();
listPropertyInfo.TryAdd(typ.Name, property);
}
return property.First(p => p.Name == parameterName).GetValue(obj, null).ToString();
}
listPropertyInfo is a cache to avoid reflection performance issue
public static void SetPropertyValue<T>(this T obj, string parameterName, object value)
{
PropertyInfo[] property = null;
Type typ = obj.GetType();
if (listPropertyInfo.ContainsKey(typ.Name))
{
property = listPropertyInfo[typ.Name];
}
else
{
property = typ.GetProperties();
listPropertyInfo.TryAdd(typ.Name, property);
}
if (value == DBNull.Value)
{
value = null;
}
property.First(p => p.Name == parameterName).SetValue(obj,value, null);
}
I used the same trick for setters
I have this method and want to get all properties from the FieldInfos? How to get it?
private static void FindFields(ICollection<FieldInfo> fields, Type t)
{
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
foreach (var field in t.GetFields(flags))
{
fields.Add(field);
}
var baseType = t.BaseType;
if (baseType != null)
{
FindFields(fields, baseType);
}
}
var fields = new Collection<FieldInfo>();
FindFields(fields, this.GetType());
Thanks.
Best regards.
To get the value of a field for a specific object use GetValue and pass the object for which you want to get the value.
var fields = new Collection<FieldInfo>();
FindFields(fields, this.GetType());
foreach (var field in fields)
{
Console.WriteLine( "{0} = {1}", field.Name , field.GetValue(this));
}