首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关于在c# (.NET 2.0)中在运行时读取类的属性的问题?

关于在c# (.NET 2.0)中在运行时读取类的属性的问题?
EN

Stack Overflow用户
提问于 2009-11-19 04:56:39
回答 7查看 1.5K关注 0票数 0

假设我有一个有3个属性的类。直到运行时我才知道要读取哪个属性。有没有办法在不使用switch/if-else语句的情况下做到这一点?

如果将DataTable视为一个类,那么可以动态地处理列,这是可行的,但我无法将我的类更改为DataTable (长篇大论)。

对于DataTable,代码为:

代码语言:javascript
运行
复制
string columnName = "Age";
int age = dataRow[columnName];

这在类中是怎么可能的?

代码语言:javascript
运行
复制
string propertyName = "Baka";
int age = item.getProperty(propertyName)  // ???
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2009-11-19 05:02:29

之前回答了一个类似的问题。这是一个代码示例,并链接到另一个问题。Hiding Public Functions

您最感兴趣的将是System.Reflection.PropertyInfo类的GetValue方法。

C#代码示例

代码语言:javascript
运行
复制
public bool SetProperty(object obj, string PropertyName, object val)
{
    object property_value = null;
    System.Reflection.PropertyInfo[] properties_info = obj.GetType.GetProperties;
    System.Reflection.PropertyInfo property_info = default(System.Reflection.PropertyInfo);
    
    foreach (System.Reflection.PropertyInfo prop in properties_info) {
        if (prop.Name == PropertyName) property_info = prop; 
    }
    
    if (property_info != null) {
        try {
            property_info.SetValue(obj, val, null);
            return true;
        }
        catch (Exception ex) {
            return false;
        }
    }
    else {
        return false;
    }
}

public object GetProperty(object obj, string PropertyName)
{
    object property_value = null;
    System.Reflection.PropertyInfo[] properties_info = obj.GetType.GetProperties;
    System.Reflection.PropertyInfo property_info = default(System.Reflection.PropertyInfo);
    
    foreach (System.Reflection.PropertyInfo prop in properties_info) {
        if (prop.Name == PropertyName) property_info = prop; 
    }
    
    if (property_info != null) {
        try {
            property_value = property_info.GetValue(obj, null);
            return property_value;
        }
        catch (Exception ex) {
            return null;
        }
    }
    else {
        return null;
    }
}

VB代码示例

代码语言:javascript
运行
复制
Public Function SetProperty(ByVal obj As Object, ByVal PropertyName As String, ByVal val As Object) As Boolean
    Dim property_value As Object
    Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
    Dim property_info As System.Reflection.PropertyInfo

    For Each prop As System.Reflection.PropertyInfo In properties_info
        If prop.Name = PropertyName Then property_info = prop
    Next

    If property_info IsNot Nothing Then
        Try
            property_info.SetValue(obj, val, Nothing)
            Return True
        Catch ex As Exception
            Return False
        End Try
    Else
        Return False
    End If
End Function

Public Function GetProperty(ByVal obj As Object, ByVal PropertyName As String) As Object
    Dim property_value As Object
    Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
    Dim property_info As System.Reflection.PropertyInfo

    For Each prop As System.Reflection.PropertyInfo In properties_info
        If prop.Name = PropertyName Then property_info = prop
    Next

    If property_info IsNot Nothing Then
        Try
            property_value = property_info.GetValue(obj, Nothing)
            Return property_value
        Catch ex As Exception
            Return Nothing
        End Try
    Else
        Return Nothing
    End If
End Function
票数 1
EN

Stack Overflow用户

发布于 2009-11-19 04:59:15

进行这种运行时评估的最简单方法是使用Reflection

根据您使用的.NET版本,您可以向类添加方法或使用扩展方法。

票数 2
EN

Stack Overflow用户

发布于 2009-11-19 05:06:35

编辑:我刚刚读到了你关于不使用开关的评论,但我还是会把它放在这里,以防万一……跳到我的第二个答案。

可能是这样的:

代码语言:javascript
运行
复制
public enum Property { Name, Age, DateOfBirth};

public T GetProperty<T>(Property property)
{
    switch (property)
    {
       case Property.Name:
          return this.Name;
       // etc.
    }
}

我对遗传学不是很在行,所以也许有人可以帮我处理回归的角色。

或者,您可以将一个字段添加到DataRow类型的类中,并使用索引器访问它:

代码语言:javascript
运行
复制
public class MyClass
{
   DataTable myRow = null;
   MyClass(DataRow row)
   {
      myRow = row;
   }

   public object this[string name]
   {
      get
      {
         return this.myRow[name];
      }
      set
      {
         this.myRow[name] = value;
      }
   }
}

用法:

代码语言:javascript
运行
复制
MyClass c = new MyClass(table.Rows[0]);
string name = c["Name"].ToString();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1759026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档