簡體   English   中英

是否有可能從PropertyInfo獲得“對象”?

[英]Is it possible to get an 'object' from a PropertyInfo?

在我的precent問題中,我想通過反射檢索一些值。 現在我希望通過反射將對象設置為值。

我想寫這個:

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);

PropertyInfo包含一種ColumnsCollection。 我只是想將我的PropertyInfo“映射”到一個對象之后定義一些屬性:例如:

cols[prop.Nom].Hidden = false;

可能嗎 ?

最好的祝福,

弗洛里安

編輯:我嘗試了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>();

但拋出了TargetException

所以你已經有一個類型為ColumnsCollectionPropertyInfo對象?

您可以使用以下代碼獲取並修改它:

var original = GetYourObject();
PropertyInfo Info = GetYourPropertyInfo(original);
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection;

基本上,您只需要將原始對象傳遞回PropertyInfo的GetValue方法,該方法將返回一個對象。 只需將其轉換為ColumnsCollection ,您應該進行排序。

更新:

根據您的更新,您應該這樣做:

object original = grille.DisplayLayout.Bands[0];
PropertyInfo info = original.GetProperty("Columns");

ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection;

您必須從不同類型的對象獲取Info PropertyInfo 雖然我認為我們在這里解決了錯誤的問題。 我不明白你想要實現的目標。 為什么不直接修改grille.DisplayLayout.Bands[0].Columns

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM