簡體   English   中英

C#反射。 設置TableAdapter ConnectionString

[英]C# Reflection. Set TableAdapter ConnectionString

我希望有人可以對此提供幫助。 我一直在嘗試為WinForm創建一個新的基類。 我想做的是讓該基類遍歷它所具有的所有tableadapters並更新其連接字符串,而無需任何人向該表單添加任何代碼。 他們只是將tableadapters放在窗體上,而不必擔心連接字符串設置,因為它們都是在基類中處理的。

我遇到的問題是我的反射代碼可以找到合適的屬性,但是無法設置它。 有人可以幫忙嗎?

下面是代碼(已更新)

public class cFormWS : Form
{
    public string ConnectionStringToUse { get; set; }

    public cFormWS()
    {
        Load += cFormWS_Load;
    }

    void cFormWS_Load(object sender, EventArgs e)
    {
        InitiliseTableAdapters();
    }

    private void InitiliseTableAdapters()
    {          
        var ListOfComponents = EnumerateComponents();

        foreach (var ItemComp in ListOfComponents)
        {
            if (ItemComp.ToString().ToLower().EndsWith("tableadapter"))
            {
                var ItemCompProps = ItemComp.GetType().GetRuntimeProperties();

                var TASQLConnection = ItemCompProps.FirstOrDefault(w => w.PropertyType == typeof(System.Data.SqlClient.SqlConnection));

                if (TASQLConnection != null)
                {
                    var property = typeof(System.Data.SqlClient.SqlConnection).GetProperty("ConnectionString");

                    // How do I set the value ?

                    string value = "some new connection string";

                    var ConvertedProperty = Convert.ChangeType(value, property.PropertyType);

                    // tried seting value.  not working "object does not match target type"
                    property.SetValue(TASQLConnection, ConvertedProperty, null);


                    //// tried using a method.  not working "object does not match target type"
                    //var m = property.SetMethod;
                    //ParameterInfo[] parameters = m.GetParameters();
                    //m.Invoke(m, parameters); // m.Invoke(this, parameters); // m.Invoke(ItemComp, parameters);
                }                      
            }                
        }
    }

    private IEnumerable<Component> EnumerateComponents()
    {
        return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
               where typeof(Component).IsAssignableFrom(field.FieldType)
               let component = (Component)field.GetValue(this)
               where component != null
               select component;
    }  

編輯:

當您執行SetValue ,您需要傳入要設置其屬性的對象。

  • 在您的第一個示例代碼中,您傳入了ItemComp :這是不正確的,因為ConnectionStringSqlConnection的屬性,而SqlConnectionItemComp的屬性
  • 在您編輯的問題(和我的原始答案)中,您傳入TASqlConnection 但是,這不是對象,而是基於對象的PropertyInfo
  • 正確的方法是從ItemComp對象獲取值並將其傳遞給:

property.SetValue(TASQLConnection.GetValue(ItemComp), ConvertedProperty, null);

原始(錯誤)答案:

您正在嘗試設置ItemCompConnectionString屬性。 ConnectionString不是TableAdapter的屬性,而是SqlConnection的屬性(這是TableAdapter的屬性)。

設置屬性的正確方法是:

property.SetValue(TASQLConnection, ConvertedProperty, null);

暫無
暫無

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

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