Flo*_*ian 5 c# reflection system.reflection
在我之前提出的问题中,我想通过反思来获取一些价值。现在,我想通过反射为对象设置值。
我想写这个:
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
if (grille != null)
{
// I want to write MapPropertyInfo method
ColumnsCollection cols = MapPropertyInfo(Info);
Run Code Online (Sandbox Code Playgroud)
PropertyInfo包含一种ColumnsCollection。我只想将我的PropertyInfo映射到一个对象,以便在之后定义一些属性,例如:
cols[prop.Nom].Hidden = false;
Run Code Online (Sandbox Code Playgroud)
可能吗 ?
最好的祝福,
弗洛里安
编辑:我尝试了GenericTypeTea解决方案,但是我有一些问题。这是我的代码段:
private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info)
{
UltraGrid grille = (UltraGrid)control;
ColumnsCollection c = grille.DisplayLayout.Bands[0].Columns;
// Throw a not match System.Reflection.TargetException
ColumnsCollection test = Info.GetValue(c,null) as ColumnsCollection;
SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>();
Run Code Online (Sandbox Code Playgroud)
但是抛出了TargetException
那么你已经有一个PropertyInfo类型的对象了ColumnsCollection?
您可以使用以下代码获取并修改它:
var original = GetYourObject();
PropertyInfo Info = GetYourPropertyInfo(original);
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection;
Run Code Online (Sandbox Code Playgroud)
基本上,您只需将原始对象传递回 的PropertyInfoGetValue 方法,该方法将返回一个对象。只需将其转换为 theColumnsCollection即可对您进行排序。
更新:
根据您的更新,您应该执行以下操作:
object original = grille.DisplayLayout.Bands[0];
PropertyInfo info = original.GetProperty("Columns");
ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection;
Run Code Online (Sandbox Code Playgroud)
Info PropertyInfo您必须从不同类型的对象中获取您的内容。尽管我认为我们在这里解决了错误的问题。我不明白你想达到什么目的。为什么不grille.DisplayLayout.Bands[0].Columns直接修改呢?