假设我有一个有3个属性的类。直到运行时我才知道要读取哪个属性。有没有办法在不使用switch/if-else语句的情况下做到这一点?
如果将DataTable视为一个类,那么可以动态地处理列,这是可行的,但我无法将我的类更改为DataTable (长篇大论)。
对于DataTable,代码为:
string columnName = "Age";
int age = dataRow[columnName];这在类中是怎么可能的?
string propertyName = "Baka";
int age = item.getProperty(propertyName) // ???发布于 2009-11-19 05:02:29
之前回答了一个类似的问题。这是一个代码示例,并链接到另一个问题。Hiding Public Functions
您最感兴趣的将是System.Reflection.PropertyInfo类的GetValue方法。
C#代码示例
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代码示例
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发布于 2009-11-19 04:59:15
进行这种运行时评估的最简单方法是使用Reflection。
根据您使用的.NET版本,您可以向类添加方法或使用扩展方法。
发布于 2009-11-19 05:06:35
编辑:我刚刚读到了你关于不使用开关的评论,但我还是会把它放在这里,以防万一……跳到我的第二个答案。
可能是这样的:
public enum Property { Name, Age, DateOfBirth};
public T GetProperty<T>(Property property)
{
switch (property)
{
case Property.Name:
return this.Name;
// etc.
}
}我对遗传学不是很在行,所以也许有人可以帮我处理回归的角色。
或者,您可以将一个字段添加到DataRow类型的类中,并使用索引器访问它:
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;
}
}
}用法:
MyClass c = new MyClass(table.Rows[0]);
string name = c["Name"].ToString();https://stackoverflow.com/questions/1759026
复制相似问题