Accessing object properties from string representations
Tag : chash , By : gorbiz
Date : March 29 2020, 07:55 AM
help you fix your problem After hitting my head against the problem for a while and hoping on a train home last night I decided that I would try and bash out an answer. Using a combination of Jon's pointers and Frederik's use of the PropertyInfo class and keeping the original idea of switching on the underlying object type, this is what I came up with ... private void Sort_version2(string propertyName)
{
// convert to list
List<MyClass> myCurrentClass = Items as List<MyClass>;
string typeOfProperty;
PropertyInfo pi;
// sort
if ((myCurrentClass != null) && (MyClass.HasDetailAndExtract(propertyName, out typeOfProperty, out pi)))
{
switch(typeOfProperty)
{
case "System.String":
myCurrentClass.Sort(delegate(MyClass one, MyClass two)
{
return
Comparer<string>.Default.Compare(pi.GetValue(one, null).ToString(),
pi.GetValue(two, null).ToString());
});
break;
case "System.Int32":
myCurrentClass.Sort(delegate (MyClass one, MyClass two)
{
return
Comparer<int>.Default.Compare(
Convert.ToInt32(pi.GetValue(one, null)),
Convert.ToInt32(pi.GetValue(two, null)));
});
break;
default:
throw new NotImplementedException("Type of property not implemented yet");
}
}
}
|
Accessing string representations of constant field values in java
Tag : java , By : user109127
Date : March 29 2020, 07:55 AM
Any of those help If you can change the class which contains these constants, it would be better to make it an enum with a value() method. Otherwise, I would suggest building a Map once using reflection, and then just doing map lookups:Map<Integer, String> valueToStringMap = new HashMap<>();
for (Field field : Foo.class.getFields()) {
int modifiers = field.getModifiers();
if (field.getType() == Integer.class && Modifier.isPublic(modifiers)
&& Modifier.isStatic(modifiers)) {
valueToStringMap.put((Integer) field.get(null), field.getName());
}
}
|
Accessing the properties of an object with a string in Swift
Date : March 29 2020, 07:55 AM
wish of those help There is no way to do it that would work for any Swift class. However, you can do this: class User: NSObject {
dynamic var username: String = "admin"
dynamic var password: String = "123456"
}
let u = User()
u.valueForKey("username") // admin
|
C# Building Expressions in Linq-to-Entities, Searching all properties' string representations in a type
Tag : chash , By : Robert MacGregor
Date : March 29 2020, 07:55 AM
it fixes the issue I do this without Equals because doesn't in the scope and i will need create some attr or flag to set what prop will be filtered Exactly or Likely, and i just go 1 level in depth of the navigations, so maybe this can help you to understand: private static readonly MethodInfo ToStringMethod = typeof(object).GetMethod("ToString");
private static readonly MethodInfo StringContainsMethod = typeof(string).GetMethod("Contains");
public static Expression<Func<T, bool>> BuildFilterPredicate<T>(string q)
{
var query = Expression.Constant(q);
var type = typeof(T);
var lambdaParam = Expression.Parameter(type);
var predicates = type.GetProperties().SelectMany(p => PredicateContainsBuilder(lambdaParam, p, query)).ToList();
Expression body = predicates[0];
body = predicates.Skip(1).Aggregate(body, Expression.OrElse);
return Expression.Lambda<Func<T, bool>>(body, lambdaParam);
}
private static IEnumerable<MethodCallExpression> PredicateContainsBuilder(Expression lambdaParam, PropertyInfo prop, Expression query)
{
if (prop.PropertyType.IsClass)
return new List<MethodCallExpression> { Expression.Call(Expression.Call(Expression.Property(lambdaParam, prop), ToStringMethod), StringContainsMethod, query) };
var properties = prop.PropertyType.GetProperties();
return properties.Select(p => Expression.Call(Expression.Call(Expression.Property(lambdaParam, p), ToStringMethod), StringContainsMethod, query)).ToList();
}
|
PHP accessing deeper object properties through string variable
Date : March 29 2020, 07:55 AM
With these it helps I have this simple function to not get any warnings when a property doesn't exist: , You can explode argument by -> and iterate it. try below solution: function getProperty($object, $property, $default = ""){
$properties = explode('->', $property);
if($properties){
foreach($properties as $val){
if(isset($object->$val)){
$object = $object->$val;
} else{
break;
}
}
}
if (!is_object($object) && $object) return $object;
return $default;
}
$o = new stdClass();
$o->name = "Peter";
echo $name = getProperty($o, 'name', 'No name'); //output: Peter
echo $email = getProperty($o, 'email', 'No email'); //output: No email
$o = new stdClass();
$o->wife = new stdClass();
$o->wife->name = "Olga";
echo getProperty($o, "wife->name", "No name"); //output: Olga
|