Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
public static class Reflection
|
||||
{
|
||||
public static void Main() {
|
||||
var t = new TestClass();
|
||||
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||
foreach (var prop in GetPropertyValues(t, flags)) {
|
||||
Console.WriteLine(prop);
|
||||
}
|
||||
foreach (var field in GetFieldValues(t, flags)) {
|
||||
Console.WriteLine(field);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(string name, object value)> GetPropertyValues<T>(T obj, BindingFlags flags) =>
|
||||
from p in typeof(T).GetProperties(flags)
|
||||
where p.GetIndexParameters().Length == 0 //To filter out indexers
|
||||
select (p.Name, p.GetValue(obj, null));
|
||||
|
||||
public static IEnumerable<(string name, object value)> GetFieldValues<T>(T obj, BindingFlags flags) =>
|
||||
typeof(T).GetFields(flags).Select(f => (f.Name, f.GetValue(obj)));
|
||||
|
||||
class TestClass
|
||||
{
|
||||
private int privateField = 7;
|
||||
public int PublicNumber { get; } = 4;
|
||||
private int PrivateNumber { get; } = 2;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue