简体   繁体   English

如何获取 C# 模型中的密钥?

[英]How do I get the keys inside a C# model?

I want to have a loop where I can get every object key in my model.我想要一个循环,我可以在其中获取模型中的每个对象键。

I have this model:我有这个模型:

public class RoleModel
{
    public int ID { get; set; }
    public string RoleName { get; set; }
    public UserRoleModel TrackAndTrace { get; set; }
    public UserRoleModel MailProcessing { get; set; }
}

I want to achieve something like:我想实现以下目标:

for (var i = 0, len = Object.keys(RoleModel).length; i < len; i++) {

  Console.Writeline(Object.keys(RoleModel)[i])

}

which would return哪个会返回

ID
RoleName
TrackAndTrace 
MailProcessing 

So, you need a Reflection.所以,你需要一个反思。 Reflection is Virtual Machine API.反射是虚拟机 API。 You can use it for get metadata of class.您可以使用它来获取类的元数据。 Please see example:请看例子:

PropertyInfo[] properties = typeof(RoleModel).GetProperties();
foreach (PropertyInfo property in properties)
{
    Console.Writeline(property.Name);
}
PropertyInfo[] properties = typeof(RoleModel).GetProperties();
foreach (PropertyInfo property in properties)
{
    Console.Writeline(property.Name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中如何从字典中获取键列表? - In C# how do I get a list of the keys from a dictionary? 如何通过c#中的snmp获取打印机型号? - How do i get printer model via snmp in c#? 在C#中,如何获取数组内部对象的属性? - In C# how do I get the properties of an object that is inside an array? 在 c# 中,如何测试/获取/设置可能存在或不存在的注册表项? - In c#, how do I test/get/set Registry keys that may or may not exist? 如何有效地提取 C# 中的嵌套 Json 键? - How do I extract nested Json Keys in C# effeciently? C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC 如何使用C#将模型放入我的视图? - How do I get my model into my view using C#? 如何创建 C# class model 以获取 Z3501BB093D363810B671059B9CFED3FZ 给定格式的序列化请求? - How do I create C# class model to get XML serialized request in given format? 如何在C#中获得针对Cloud Machine Learning Engine上的模型的在线预测? - How do I get online predictions in C# for my model on Cloud Machine Learning Engine? C# WCF - 如何使用 get 和 set 将传入的 883812976388 字符串转换为 model? - C# WCF - How do I convert an incoming xml string into a model with get and set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM