[英]Method to get fields and properties by using reflection c#
i use two arrays in my code:我在代码中使用了两个数组:
var fields = typeof(MyDtoClass).GetProperties();
var fieldsOfDtoClass = typeof(MyDtoClass).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
but i using this to generate one table and it is possible to out of sync.但我用它来生成一张表,可能会不同步。 How can i use one array, but can get in one place properties, and in another place fields this such flags?
我如何使用一个数组,但可以在一个地方获得属性,而在另一个地方获得这样的标志? should i look in GetMembers or smth?
我应该看看 GetMembers 还是 smth? any help is much appreciated
任何帮助深表感谢
Using class name使用类名
typeof(YourClass).GetMethod("METHOD_NAME");
typeof(YourClass).GetProperty("PROPERTY_NAME");
Using instance使用实例
yourInstance.GetType().GetMethod("METHOD_NAME");
yourInstance.GetType().GetProperty("PROPERTY_NAME");
To enumerate all properties枚举所有属性
foreach (var property in typeof(YourClass)) {
Console.WriteLine(property);
}
The same way to can enumerate them using Linq可以使用 Linq 枚举它们的相同方法
typeof(YourClass).GetMethods()
.Select(method => method.Name == "GetType");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.