How to get the PropertyInfo for a DataRow from its PropertyDescriptor.

//pd is a PropertyDescriptor
var propertyinfo = pd.ComponentType.GetProperty(pd.Name);

the above code works fine for a list collection, but it not works while am working with DataTable.

Any idea on this?

有帮助吗?

解决方案

PropertyInfo is the reflection world, where types have explicit CLI properties. DataTable does not belong to that world; it uses the System.ComponentModel flexible property model, via TypeDescriptor, PropertyDescriptor, etc. Basically: there is no property in the CLI sense. PropertyDescriptor can be used (and is used) to describe "properties" in a more flexible, dynamic sense, where the layout is not specified as a type, but is custom-defined, often on-the-fly at runtime.

So no: you can't do this. The question does not make sense; or at least, in the general case it doesn't. There is also "typed datasets", but frankly I strongly recommend staying far far away from them.

Incidentally, you can invent your own pseudo-properties for any type - there are extension points for this (TypeDescriptor, ITypedList, ICustomTypeDescriptor, TypeConverter, etc); but only code that explicitly uses System.ComponentModel will ever see them.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top