簡體   English   中英

如何通過字符串正確訪問UserControl公共屬性

[英]How Do I Properly Access a UserControl Public Property by String

我在頁面上有一組用戶控件,這些控件根據條件動態加載以運行各種報告(條件驅動程序)。 每個控件都有一個或多個公開的屬性,這些屬性將用於從數據庫查詢中獲取數據。 因為每個報表的控件都不相同,所以我編寫了一個過程來按名稱訪問相應控件的屬性,因此可以將其發送到(C#)后面的代碼中的數據庫查詢中。 我已完成所有設置以訪問公共財產,如下所示:

stringVal = userControl.Attributes[stringName].ToString();

告訴我我需要更新一個對象。 我不明白如何需要通過字符串名稱動態訪問該屬性。 在我的即時窗口中,我可以看到我想要的屬性。 但是,它不是control.Attributes.Count = 0的“屬性”。 因此,如何正確設置此設置,以便可以通過字符串名稱訪問它? 我需要用某種東西裝飾酒店嗎?

先感謝您。

您需要探索反思。 它涉及操縱.NET類型(類,結構,枚舉等)的元數據。 但是,如果您在具有部分信任的共享托管中運行應用程序,則可能根本無法運行任何類型的反射代碼(服務器上的安全性限制)。 如果是這種情況,請首先在您的主機上通過一個小型/快速示例進行測試,然后做出適當的決定:是重新設計應用程序還是更改主機。

與反射有關的代碼段可能對您有用(將其粘貼在用戶控件類中的某個位置,因為this.GetType()重要:

var properties = this.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

foreach (var p in properties)
{
    string propertyName = p.Name;
}

還有其他獲取類型的方法。 例如typeof(yourclassname) ; 您還可以從給定的程序集(dll)反映/獲取所有類型,等等。

在下面,我編寫了一個包裝器類,您可以使用它來通過字符串名稱設置/獲取包裝類的公共字段或屬性。

首先,讓我們看一下如何在具有公共字段或屬性StartDate和SocialSecurityNumber的類上使用它。

// First create the instance we'll want to play with
MyUserControl myControlInstance = new MyUsercontrol();
// MyUserContol has two properties we'll be using
// DateTime StartDate
// int SocialSecurityNumber

// Now we're creating a map to facilitate access to the properties
// of "myControlInstance" using strings
PropertyMap<MyUserControl> map = 
             new PropertyMap<MyUserControl>(myControlInstance);

// Since the map is directed toward "myControlInstance"
// this line is equivalent to:
// myControlInstance.StartDate = Datetime.Now;
map.Set<DateTime>("StartDate", DateTime.Now);

// This line is equivalent to:
// ssn = myUsercontrol.SocialSecurityNumber;
int ssn = map.Get<int>("SocialSecurityNumber");

現在介紹如何實現:

public class PropertyMap<T>
{
    readonly T Instance;

    public PropertyMap(T instance)
    {
        Instance = instance;
    }

    public U Get<U>(string PropertyName)
    {
        // Search through the type's properties for one with this name
        // Properties are things with get/set accessors
        PropertyInfo property = typeof(T).GetProperty(PropertyName);
        if (property == null)
        {
            // if we couldn't find a property, look for a field.
            // Fields are just member variables, but you can only
            // manipulate public ones like this.
            FieldInfo field = typeof(T).GetField(PropertyName);
            if (field == null)
                throw new Exception("Couldn't find a property/field named " + PropertyName);
            return (U)field.GetValue(Instance);
        }
        return (U)property.GetValue(Instance, null);
    }

    public void Set<U>(string PropertyName, U value)
    {
        // Search through the type's properties for one with this name
        PropertyInfo property = typeof(T).GetProperty(PropertyName);
        if (property == null)
        {
            // if we couldn't find a property, look for a field.
            FieldInfo field = typeof(T).GetField(PropertyName);
            if (field == null)
                throw new Exception("Couldn't find a property/field named " + PropertyName);
            field.SetValue(Instance, value);
            return;
        }
        property.SetValue(Instance, value, null);
    }
}

暫無
暫無

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

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