Accessing object properties from string representations - c#

In actionscript an object's property can be accesses in this way:
object["propertyname"]
Is something like this possible in c#, without using reflection?

No, you have to use reflection.
If could at most create an helper extension method like in this example:
using System;
static class Utils {
public static T GetProperty<T>(this object obj, string name) {
var property = obj.GetType().GetProperty(name);
if (null == property || !property.CanRead) {
throw new ArgumentException("Invalid property name");
}
return (T)property.GetGetMethod().Invoke(obj, new object[] { });
}
}
class X {
public string A { get; set; }
public int B { get; set; }
}
class Program {
static void Main(string[] args) {
X x = new X() { A = "test", B = 3 };
string a = x.GetProperty<string>("A");
int b = x.GetProperty<int>("B");
}
}
This is not good, however.
First because you are turning compile-time errors in runtime errors.
Second, the performance hit from reflection is unjustified in this case.
I think that the best advice here is that you should not try to program in C# as if it was ActionScript.

You can define indexer in your class:
public class IndexerTest
{
private Dicionary<string, string> keyValues = new ...
public string this[string key]
{
get { return keyValues[key]; }
set { keyValues[key] = value; }
}
}
And use it like this:
string property = indexerTest["propertyName"];

Related

Create Expression that accesses a property

I have a following class:
internal class Sensors
{
public JsonSensor<double> IOPcwFlSpr { get; set; } = new JsonSensor<double>();
}
internal class JsonSensor<TType> : IJsonSensor
{
public TType Value { get; set; }
}
I want to build an expression that retrieves that property.
private static readonly List < PropertyInfo > Properties;
static SensorFactory() {
Properties = typeof(Json.Sensors).GetProperties().ToList();
}
public void Test(Json.Sensors jsonUpdate) {
foreach(var property in Properties) {
var getterMethodInfo = property.GetGetMethod();
var parameterExpression = Expression.Parameter(jsonUpdate.GetType(), "x");
var callExpression = Expression.Call(parameterExpression, getterMethodInfo);
var lambda = Expression.Lambda < Func < JsonSensor < double >>> (callExpression);
var r = lambda.Compile().Invoke();
}
}
This throws:
System.InvalidOperationException : variable 'x' of type 'Sensors'
referenced from scope '', but it is not defined
Which makes sense, because I never assigned 'x' with an actual object. How do I add the 'parameter object'?
The key when using expression trees like this is to compile it once using a parameter (ParameterExpression), creating a Func<Foo,Bar> that takes your input (Foo) and returns whatever you wanted (Bar). Then reuse that compiled delegate many times, with different objects.
I can't see exactly what you're trying to do, but I'm guessing it would be something like:
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace Json
{
static class P
{
static void Main()
{
var obj = new Sensors { IOPcwFlSpr = { Value = 42.5 }, Whatever = { Value = 9 } };
foreach(var pair in SomeUtil.GetSensors(obj))
{
Console.WriteLine($"{pair.Name}: {pair.Value}");
}
}
}
public class Sensors
{
public JsonSensor<double> IOPcwFlSpr { get; set; } = new JsonSensor<double>();
public JsonSensor<int> Whatever { get; set; } = new JsonSensor<int>();
}
public interface IJsonSensor
{
public string Value { get; }
}
public class JsonSensor<TType> : IJsonSensor
{
public TType Value { get; set; }
string IJsonSensor.Value => Convert.ToString(Value);
}
public static class SomeUtil
{
private static readonly (string name, Func<Sensors, IJsonSensor> accessor)[] s_accessors
= Array.ConvertAll(
typeof(Sensors).GetProperties(BindingFlags.Instance | BindingFlags.Public),
prop => (prop.Name, Compile(prop)));
public static IEnumerable<(string Name, string Value)> GetSensors(Sensors obj)
{
foreach (var acc in s_accessors)
yield return (acc.name, acc.accessor(obj).Value);
}
private static Func<Sensors, IJsonSensor> Compile(PropertyInfo property)
{
var parameterExpression = Expression.Parameter(typeof(Json.Sensors), "x");
Expression body = Expression.Property(parameterExpression, property);
body = Expression.Convert(body, typeof(IJsonSensor));
var lambda = Expression.Lambda<Func<Json.Sensors, IJsonSensor>>(body, parameterExpression);
return lambda.Compile();
}
}
}

Func list for storing function pointer and parameter value

I want to store a list of functions with their parameter value and later when I am done adding function to the list. I want to execute all in the order I have added.
For example, I want to use func instead of action and don't want to create anonymous functions while calling parametered function:
Dynamic function list class to hold a function list and execute it later.
class DynamicFunctionList
{
public List<Action> DynamicList = new List<Action>();
public void Execute()
{
foreach (var obj in DynamicList)
{
obj();
}
}
}
some class with functions
public class SomeClass
{
public void PrintHello()
{
Console.Write("Hello");
}
public void PrintBye()
{
Console.Write("Print Bye");
}
public int GetPrinterValue()
{
return 2;
}
public int Add(int a, int b)
{
return (a + b);
}
}
And this is how you will use it
public static void MainClass()
{
var first = 0;
var second = 0;
var dfList = new DynamicFunctionList();
var sClass = new SomeClass();
dfList.DynamicList.Add(() => first = sClass.GetPrinterValue()); // problem line
dfList.DynamicList.Add(sClass.PrintBye);
dfList.DynamicList.Add(sClass.PrintHello);
dfList.DynamicList.Add(() => second = sClass.Add(2, 3)); // problem
dfList.Execute();
}
I do something like that:
public void main()
{
List<MethodInvoker> methods = new List<MethodInvoker>();
methods.Add(new MethodInvoker(SomeMethod));
foreach (var method in methods)
{
method.Invoke();
}
}
public void SomeMethod()
{
//... do something
}
EDIT 1:
You can use MethodBase.Invoke from System.Reflection namespace (more infos: https://msdn.microsoft.com/en-us/library/a89hcwhh%28v=vs.110%29.aspx)
You can do something like that:
public class DynamicMethod
{
public string ClassName { get; set; }
public string MethodName { get; set; }
public object[] Parameters { get; set; }
public static object InvokeMethod(DynamicMethod methodInfo)
{
var magicType = Type.GetType(methodInfo.ClassName);
var magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
var magicInstance = magicConstructor.Invoke(new object[] {});
var magicMethod = magicType.GetMethod(methodInfo.MethodName);
return magicMethod.Invoke(magicInstance, methodInfo.Parameters);
}
}
public class Example
{
public static void main()
{
var d1 = new DynamicMethod
{
ClassName = "SomeClass",
MethodName = "Add",
Parameters = new object[] { 1, 2 }
};
var returnedValue = DynamicMethod.InvokeMethod(d1);
Console.WriteLine(returnedValue.ToString());
}
Also you can add more information to this class, like some way to store the returned type to do the properly cast.

How to trigger a Generic Class method recursively changing the type of T?

I've created a Generic Class to parse some data into another instance of a class (MyClass1). Since MyClass1 has only built-in C# types, my GenericMethod works fine. The problem starts to grow when MyClass1 has another MyClass2 property and I still want to invoke my GenericMethod to parse my data.
I can't trigger my Generic Class method inside its scope since I need to change the type of T. Is there any way to solve this problem?
public class MyClass1
{
public int MyIntProperty { get; set; }
public string MyStringProperty { get; set; }
public MyClass2 MyClass2Property { get; set; }
}
public class MyClass2
{
public int MyOtherIntProperty { get; set; }
public string MyOtherStringProperty { get; set; }
public bool MyOtherBoolProperty { get; set; }
}
public class MyGenericClass<T> where T : class
{
public static T MyGenericMethod()
{
T o = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] pi = typeof(T).GetProperties();
for(int i = 0; i < pi.Count(); i++)
{
if(pi[i].Name == "MyClass2Property")
{
//How to proceed ?
MyGenericClass<???>.MyGenericMethod();
}
else
{
pi[i].SetValue(o, Convert.ChangeType(someValue, pi[i].PropertyType), null);
}
}
}
}
public static void Main(string[] args)
{
MyClass1 mc1 = MyGenericClass<MyClass1>.MyGenericMethod();
//Do something with mc1
}
You can look at this post
and maybe try something like this
public static class MyGenericClass<T> where T : class
{
public static T MyGenericMethod()
{
T o = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] pi = typeof(T).GetProperties();
for(int i = 0; i < pi.Count(); i++)
{
if(pi[i].Name == "MyClass2Property")
{
//How to proceed ?
Type t = typeof (MyGenericClass<>);
Type genericType = t.MakeGenericType(new System.Type[] { pi[i].PropertyType });
var c = Activator.CreateInstance(genericType);
dynamic mgm = Convert.ChangeType(c, genericType);
mgm.MyGenericMethod();
}
else
{
pi[i].SetValue(o, Convert.ChangeType(someValue, pi[i].PropertyType), null);
}
}
}
Depending on your needs, you could also define some additional meta information about the property indicating what you would like to do with it if found.
Building on others' comments and answers, here is what I came up with including an attribute decoration that allows you to dynamically build objects and has the following enhancements:
Properties can be named anything you want
No need for if statements as new properties are added.
No need for the MyGenericMethod method to ever change.
Additional meta-information can be added to the custom attribute to add further customization in the future.
Objects can be nested as deeply as needed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Dynamic;
public class MyClass1 {
public int MyIntProperty { get; set; }
public string MyStringProperty { get; set; }
[MyCustom()]
public MyClass2 MyClass2Property { get; set; }
}
public class MyClass2 {
public int MyOtherIntProperty { get; set; }
public string MyOtherStringProperty { get { return "oooh, fancy"; } set {} }
public bool MyOtherBoolProperty { get; set; }
}
public static class MyGenericClass<T> where T : class {
public static T MyGenericMethod() {
T o = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] pi = typeof(T).GetProperties();
for (int i = 0; i < pi.Count(); i++) {
if (pi[i].GetCustomAttributes(true).Any() && pi[i].GetCustomAttributes(true).Where((x) => x is MyCustomAttribute).Any()) {
//How to proceed ?
var c = Activator.CreateInstance(pi[i].PropertyType);
Type t = typeof(MyGenericClass<>);
Type genericType = t.MakeGenericType(new System.Type[] { pi[i].PropertyType });
MethodInfo m = genericType.GetMethod(MethodInfo.GetCurrentMethod().Name);
c = m.Invoke(null, null);
pi[i].SetValue(o, c, null);
} else {
//Normal property assignment.
}
}
return o;
}
}
public class Program {
public static void Main(string[] args) {
MyClass1 mc1 = MyGenericClass<MyClass1>.MyGenericMethod();
//Do something with mc1
Console.WriteLine(mc1.MyClass2Property.MyOtherStringProperty);
Console.ReadLine();
}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public class MyCustomAttribute : Attribute {
}
I tweaked this so it can be run as is.
Edit:
I also changed the code to invoke the method being called on itself to avoid a "magic string".

Get custom attribute from specific object property/field

I've been searching for a while now and tested several methods, but i didn't find the answer i was looking for. I'll try to explain.
I have an object with several fields/properties. These properties have custom attributes.
What i want is to get the custom attribute from a specific propertie without all the knowlege of the object.
The are the base classes
// FieldAttr has a public Text propery
public class TestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1
{
public string Value { get; set; }
}
public class FLDtype2
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public EntityFieldType(string txt)
{
this._text = txt;
}
public string Text { get { return this._text; } }
}
And i want to be able to do this in my application:
static void Main(string[] args)
{
TestObject test = new TestObject();
// (Option 1: preferred)
Console.WriteLine(test.testfld1.getFieldAttr().Text);
// (Option 2)
Console.WriteLine(test.getFieldAttr(test.testfld1).Text);
}
Is this possible? I've seen methods to get custom attribute values from all properties/fields of an object, but not for a specific field.
I've got a working method to get custom attribute from an enum, but wasn't able to recreate it for object fields/properties. This is because i couldn't get the name of the field i was trying to explore, because (for example) test.testfld1.ToString() give's me "ns.FLDtype1".
Looking forward for the answer :)
(and excuse my english)
Yes it is possible:
public static class Extensions
{
public static FieldAttr GetFieldAttr(
this TestObject source,
Expression<Func<TestObject,object>> field)
{
var member = field.Body as MemberExpression;
if (member == null) return null; // or throw exception
var fieldName = member.Member.Name;
var test = typeof (TestObject);
var fieldType = test.GetField(fieldName);
if (fieldType != null)
{
var attribute = fieldType.GetCustomAttribute<FieldAttr>();
return attribute;
}
return null;
}
}
Usage:
TestObject test = new TestObject();
var attr = test.GetFieldAttr(x => x.testfld3);
if(attr != null) Console.WriteLine(attr.Text);
Here is the fiddle
After another day of trial and error I decided to make use of Selman22 answer with a little modification. This is code I created:
public class TestObject : iTestObject
{
// Declare fields
[FieldAttr("prop_testfld1")]
public FLDtype1 testfld1 = new FLDtype1();
[FieldAttr("prop_testfld2")]
public FLDtype2 testfld2 = new FLDtype2();
[FieldAttr("prop_testfld3")]
public FLDtype1 testfld3;
}
public class FLDtype1 : iField
{
public string Value { get; set; }
}
public class FLDtype2 : iField
{
public Guid Value { get; set; }
}
public sealed class FieldAttr: System.Attribute
{
private string _txt;
public FieldAttr(string txt)
{
this._txt = txt;
}
public string Text { get { return this._txt; } }
}
public interface iField { }
public interface iTestObject { }
public static class Extensions
{
public static FieldAttr GetFieldAttr<T>(this T source, Expression<Func<iField>> field) where T : iTestObject
{
// Get member body. If no body present, return null
MemberExpression member = (MemberExpression)field.Body;
if (member == null) { return null; }
// Get field info. If no field info present, return null
FieldInfo fieldType = typeof(T).GetField(member.Member.Name);
if (fieldType == null) { return null; }
// Return custom attribute
return fieldType.GetCustomAttribute<FieldAttr>();
}
}
Usage:
public class Program
{
public static void Main()
{
TestObject test = new TestObject();
Console.WriteLine(test.GetFieldAttr(() => test.testfld1).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld2).Text);
Console.WriteLine(test.GetFieldAttr(() => test.testfld3).Text);
}
}
Uses:
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
I have implemented interfaces to protect the GetFieldAttr method
#Sulman22: Thnx for the response!

What data structure is appropriate for this?

Within code I want to do something like this:
item.Stage = Stage.Values.ONE;
Where Stage.Values.ONE represents some predefined Stage:
public class Stage
{
[Key]
public virtual int StageId { get; set; }
public string Name { get; set; }
public TimeSpan Span { get; set; }
}
I'm dealing with EF CodeFirst... and I have a lot of stages to define. I'm not sure if I should store the data in the database, or in the dbContext, or what, but I'm looking for the simplest implementation.
I've tried this:
I've tried the following (defining two constants):
public class Stage
{
[Key]
public virtual int StageId { get; set; }
public string Name { get; set; }
public TimeSpan Span { get; set; }
public static class Values
{
public static readonly Stage ONE = new Stage()
{
StageId = 0,
Name = "ONE",
Span = new TimeSpan(0, 0, 0)
};
public static readonly Stage TWO = new Stage()
{
StageId = 1,
Name = "TWO",
Span = new TimeSpan(0, 0, 10)
};
}
But whenever I create a new instance of an entity that has a Stage, a new Stage is added to the db. I just need a few constant stages.
Use of Stage:
public class Side
{
public Side()
{
Stage = Stage.Values.ONE; // Adds new Stage to DB, when it should be a reference to the one I defined above
}
public virtual Stage Stage { get; set; }
}
It looks a bit like an enum, and I've used a kind of 'extended enum' patter several times before with some success. Because you're refencing these values in code, it may not make sense to store them in the database as well, but it's possible if needed.
The technique is described in detail here: http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/
Basically, you create a base class which provides a number of services similar to an enum, and then to create your "enumerated class" you inherit from it and provide a bunch of static instances which call the constructor with however many properties you need to have.
To avoid link rot, here is the base class to use (just put the whole class into your project somewhere), and scroll down for your own code.
public abstract class Enumeration : IComparable
{
private readonly int _value;
private readonly string _displayName;
protected Enumeration()
{
}
protected Enumeration(int value, string displayName)
{
_value = value;
_displayName = displayName;
}
public int Value
{
get { return _value; }
}
public string DisplayName
{
get { return _displayName; }
}
public override string ToString()
{
return DisplayName;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
{
var type = typeof(T);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var instance = new T();
var locatedValue = info.GetValue(instance) as T;
if (locatedValue != null)
{
yield return locatedValue;
}
}
}
public override bool Equals(object obj)
{
var otherValue = obj as Enumeration;
if (otherValue == null)
{
return false;
}
var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = _value.Equals(otherValue.Value);
return typeMatches && valueMatches;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
{
var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
return absoluteDifference;
}
public static T FromValue<T>(int value) where T : Enumeration, new()
{
var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
return matchingItem;
}
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
{
var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName);
return matchingItem;
}
private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
{
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
if (matchingItem == null)
{
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
throw new ApplicationException(message);
}
return matchingItem;
}
public int CompareTo(object other)
{
return Value.CompareTo(((Enumeration)other).Value);
}
}
And now your code will look something like this:
public class Stage : Enumeration
{
public TimeSpan TimeSpan { get; private set; }
public static readonly Stage One
= new Stage (1, "Stage one", new TimeSpan(5));
public static readonly Stage Two
= new Stage (2, "Stage two", new TimeSpan(10));
public static readonly Stage Three
= new Stage (3, "Stage three", new TimeSpan(15));
private EmployeeType() { }
private EmployeeType(int value, string displayName, TimeSpan span) : base(value, displayName)
{
TimeSpan = span;
}
}
Once you have that set up, you can just store the .Value in the database. I'm afraid I haven't done it in EF, but in nHibernate it's reasonably straight-forward to tell a property to just store the ".Value" of the property, and you can wire it back up when you load the value by having it call:
Stage.FromValue<Stage>(intValue);
Hold the Stage as a property of your entity, use it the way you're doing and add
Ignore(x => x.Stage)
to your mapping. This will ignore this property when mapping to your database.
Edit: I misinterpreted the question.
If you want just the different stages in your database, you should put the stages in their own table with an ID, and refer to that ID trough a relationship. Every entity will hold an additional reference and you'll have to define relationships for them.
Is this what you were looking for?

Categories