class Person{
public int ID { get; set; }
public string Name { get; set; }
}
var person = new Person() { ID = 1, Name = "test"};
foreach(var property in person)
{
Console.WriteLine("property: " + property + ", value: "+ person[property]);
//may be output is:
//property: ID, value: 1
//property: Name, value: test
}
Can use reflection
var person = new Person() { ID = 1, Name = "test" };
foreach (var property in person.GetType().GetProperties())
{
Console.WriteLine("property: " + property.Name + ", value: " + property.GetValue(person));
// Outputs:
// property: ID, value: 1
// property: Name, value: test
}
Can use reflection
var person = new Person() { ID = 1, Name = "test" }; foreach (var property in person.GetType().GetProperties()) { Console.WriteLine("property: " + property.Name + ", value: " + property.GetValue(person)); // Outputs: // property: ID, value: 1 // property: Name, value: test }
I know reflection, but I think this code size is a bit bigger than javascript. so i want some syntactic sugar.
bye the way, in our project we usually use the expression tree to access the value of the property. because of better performance. but the expression tree is more complicated to write. so i want this syntactic sugar. because I believe that you can provide a better performance experience.
I think this code size is a bit bigger than javascript. so i want some syntactic sugar.
Can define an extension method once
public static class TypeExtensions
{
public static PropertyEnumerator<T> Properties<T>(this T obj) => new PropertyEnumerator<T>(obj);
public struct PropertyEnumerator<T> : IEnumerator<KeyValuePair<string, object>>
{
private T _obj;
private PropertyInfo[] _properties;
private KeyValuePair<string, object> _current;
private int _index;
public PropertyEnumerator(T obj)
{
_obj = obj;
_properties = obj.GetType().GetProperties();
_current = default;
_index = -1;
}
public PropertyEnumerator<T> GetEnumerator() => this;
public bool MoveNext()
{
_index++;
if (_index < _properties.Length)
{
var property = _properties[_index];
_current = new KeyValuePair<string, object>(property.Name, property.GetValue(_obj));
return true;
}
else
{
_current = default;
return false;
}
}
public KeyValuePair<string, object> Current => _current;
public void Dispose() { }
object IEnumerator.Current => _current;
public void Reset() => throw new NotSupportedException();
}
}
Then it becomes
foreach (var property in person.Properties())
{
Console.WriteLine("property: " + property.Key + ", value: " + property.Value);
}
// Outputs:
// property: ID, value: 1
// property: Name, value: test
For an actual change to the C# language to support a different syntax; you'd might want to raise it in the https://github.com/dotnet/csharplang repo rather than the runtime one.
ok, thanks
Most helpful comment
Can define an extension method once
Then it becomes
For an actual change to the C# language to support a different syntax; you'd might want to raise it in the https://github.com/dotnet/csharplang repo rather than the runtime one.