Getting Nested Object Property Value Using Reflection

public object GetPropertyValue(object obj, string propertyName) { foreach (var prop in propertyName.Split(‘.’).Select(s => obj.GetType().GetProperty(s))) obj = prop.GetValue(obj, null); return obj; } Thanks, I came here looking for an answer to the same problem. I ended up modifying your original method to support nested properties. This should be more robust than having to do nested method … Read more

Creating a new instance of a KClass

In your case, Java reflection might be enough: you can use MyClass::class.java and create a new instance in the same way as you would with Java reflection (see @IngoKegel’s answer). But in case there’s more than one constructor and you really need to get the primary one (not the default no-arg one), use the primaryConstructor … Read more

Attributes vs. CustomAttributes in PropertyInfo

PropertyInfo.Attributes doesn’t have anything to do with the Attribute class. Check the PropertyAttributes enumeration for values you may encounter. These are CLR implementation details that have no obvious connection to C# code. Yes, that was an unfortunate naming choice. To find attributes like your [Required] attribute you must use the CustomAttributes property.

Cast a property to its actual type dynamically using reflection

public object CastPropertyValue(PropertyInfo property, string value) { if (property == null || String.IsNullOrEmpty(value)) return null; if (property.PropertyType.IsEnum) { Type enumType = property.PropertyType; if (Enum.IsDefined(enumType, value)) return Enum.Parse(enumType, value); } if (property.PropertyType == typeof(bool)) return value == “1” || value == “true” || value == “on” || value == “checked”; else if (property.PropertyType == typeof(Uri)) return … Read more

Python super class reflection

C.__bases__ is an array of the super classes, so you could implement your hypothetical function like so: def magicGetSuperClasses(cls): return cls.__bases__ But I imagine it would be easier to just reference cls.__bases__ directly in most cases.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)