C#获取实体类属性名和值 | 遍历类对象

遍历获得一个实体类的所有属性名,以及该类的所有属性的值
//先定义一个类:

public class User
{
  public string name { get; set; }
  public string gender { get; set; }
  public string age { get; set; }
}
//实例化类,并给实列化对像的属性赋值:

User u = new User();
u.name = "ahbool";
u.gender = "男";

//输出此类的所有属性名和属性对应的值

Response.Write(getProperties(u));

//输出结果为: name:ahbool,gender:男,age:,

//遍历获取类的属性及属性的值:

public string getProperties<T>(T t)
{
  string tStr = string.Empty;
  if (t == null)
  {
    return tStr;
  }
  System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

  if (properties.Length <= 0)
  {
    return tStr;
  }
  foreach (System.Reflection.PropertyInfo item in properties)
  {
    string name = item.Name;
    object value = item.GetValue(t, null);
    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
    {
      tStr += string.Format("{0}:{1},", name, value);
    }
    else
    {
      getProperties(value);
    }
  }
  return tStr;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于使用 C# ObjectARX 进行块参照属性值过滤筛选实体,您可以按照以下步骤进行操作: 1. 获取块参照实体:使用 `ObjectId` 或 `Transaction` 对象获取块参照的 `BlockReference` 实体。 ```csharp ObjectId blockRefId = ...; // 块参照的 ObjectId Transaction tr = ...; // 当前事务 BlockReference blockRef = tr.GetObject(blockRefId, OpenMode.ForRead) as BlockReference; ``` 2. 获取块定义:从块参照实体获取块定义。 ```csharp ObjectId blockDefId = blockRef.BlockTableRecord; BlockTableRecord blockDef = tr.GetObject(blockDefId, OpenMode.ForRead) as BlockTableRecord; ``` 3. 遍历块定义中的实体遍历块定义中的实体,根据属性值进行筛选。 ```csharp foreach (ObjectId entityId in blockDef) { Entity entity = tr.GetObject(entityId, OpenMode.ForRead) as Entity; // 检查实体属性值 if (entity is AttributeDefinition attDef) { // 如果是属性定义 string attTag = attDef.Tag; string attValue = blockRef.GetAttributeValue(attTag); // 根据属性值进行筛选 if (attValue == "筛选条件") { // 符合筛选条件的处理逻辑 } } else if (entity is AttributeReference attRef) { // 如果是属性引用 string attTag = attRef.Tag; string attValue = attRef.TextString; // 根据属性值进行筛选 if (attValue == "筛选条件") { // 符合筛选条件的处理逻辑 } } else { // 非属性实体的处理逻辑 } } ``` 请注意,这只是一个简单的示例,具体的实现可能需要根据您的需求进行调整。希望能对您有所帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值