使用反射动态地将 属性 转换为其实际类型(其中实际类型是通用的)
Cast a property to its actual type dynamically using reflection (where actual type is generic)
这是一个略有不同的问题 here。
我将相同的代码修改为我的需求,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace cns01
{
class Program
{
public class ClassA<T>
{
public int IntProperty { get; set; } = 999;
}
public class ClassB<T2>
{
public ClassA<int> MyIntProperty { get; set; }
public ClassA<string> MyStringProperty { get; set; }
}
static void Main(string[] args)
{
ClassB<int> tester = new ClassB<int>();
tester.MyIntProperty = new ClassA<int>() { IntProperty = 777 };
PropertyInfo propInfo = typeof(ClassB<>).GetProperty("MyIntProperty");
dynamic property = propInfo.GetValue(tester, null); // <-- Here I get error : Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true
int result = property.IntProperty; //(*1)
}
}
}
有人知道如何将值设置(and/or 获取)结果(*1)吗?
感谢您的帮助,
提前致谢...
我的实际职位是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace cns01
{
class Program
{
public class DataFieldInfo2<T>
{
public bool IsSearchValue { get; set; } = false;
public T Value { get; set; }
}
public class SmartRowVertrag
{
public DataFieldInfo2<int> ID { get; set; }
public DataFieldInfo2<string> VSNR { get; set; }
}
static void Main(string[] args)
{
SmartRowVertrag tester = new SmartRowVertrag();
tester.ID = new DataFieldInfo2<int>() { Value = 777, IsSearchValue = false };
tester.VSNR = new DataFieldInfo2<string>() { Value = "234234234", IsSearchValue = true };
var g = RetData3(tester);
}
public static object RetData3(object fsr)
{
object retVal = new object();
var props = fsr.GetType().GetProperties();
foreach (var prop in props)
{
PropertyInfo propInfo = prop.GetType().GetProperty("IsSearchValue"); // <-- here I get null
propInfo = prop.PropertyType.GetProperty("IsSearchValue"); // here I get a propertyInfo, but...
dynamic property = propInfo.GetValue(fsr, null); // ...<-- here fires error
var result = property.IsSearchValue;
if (result == true)
{
// doThis
}
}
return retVal;
}
}
}
我怎样才能做到不出错?
“prop”似乎是一个 PropertyInfo,我不能将其用作通用 DataFieldInfo2。
将接收 属性 的代码更改为:
PropertyInfo propInfo = tester.GetType().GetProperty("MyIntProperty");
所以你使用构造泛型类型。
这是一个略有不同的问题 here。 我将相同的代码修改为我的需求,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace cns01
{
class Program
{
public class ClassA<T>
{
public int IntProperty { get; set; } = 999;
}
public class ClassB<T2>
{
public ClassA<int> MyIntProperty { get; set; }
public ClassA<string> MyStringProperty { get; set; }
}
static void Main(string[] args)
{
ClassB<int> tester = new ClassB<int>();
tester.MyIntProperty = new ClassA<int>() { IntProperty = 777 };
PropertyInfo propInfo = typeof(ClassB<>).GetProperty("MyIntProperty");
dynamic property = propInfo.GetValue(tester, null); // <-- Here I get error : Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true
int result = property.IntProperty; //(*1)
}
}
}
有人知道如何将值设置(and/or 获取)结果(*1)吗?
感谢您的帮助, 提前致谢...
我的实际职位是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace cns01
{
class Program
{
public class DataFieldInfo2<T>
{
public bool IsSearchValue { get; set; } = false;
public T Value { get; set; }
}
public class SmartRowVertrag
{
public DataFieldInfo2<int> ID { get; set; }
public DataFieldInfo2<string> VSNR { get; set; }
}
static void Main(string[] args)
{
SmartRowVertrag tester = new SmartRowVertrag();
tester.ID = new DataFieldInfo2<int>() { Value = 777, IsSearchValue = false };
tester.VSNR = new DataFieldInfo2<string>() { Value = "234234234", IsSearchValue = true };
var g = RetData3(tester);
}
public static object RetData3(object fsr)
{
object retVal = new object();
var props = fsr.GetType().GetProperties();
foreach (var prop in props)
{
PropertyInfo propInfo = prop.GetType().GetProperty("IsSearchValue"); // <-- here I get null
propInfo = prop.PropertyType.GetProperty("IsSearchValue"); // here I get a propertyInfo, but...
dynamic property = propInfo.GetValue(fsr, null); // ...<-- here fires error
var result = property.IsSearchValue;
if (result == true)
{
// doThis
}
}
return retVal;
}
}
}
我怎样才能做到不出错? “prop”似乎是一个 PropertyInfo,我不能将其用作通用 DataFieldInfo2。
将接收 属性 的代码更改为:
PropertyInfo propInfo = tester.GetType().GetProperty("MyIntProperty");
所以你使用构造泛型类型。