简体   繁体   English

C#反射。 设置TableAdapter ConnectionString

[英]C# Reflection. Set TableAdapter ConnectionString

I hope someone can help with this one. 我希望有人可以对此提供帮助。 I've been trying to create a new base class for a WinForm. 我一直在尝试为WinForm创建一个新的基类。 What I want to do is have this base class go through all the tableadapters it has on it and update their connection strings without anyone adding any code to the form. 我想做的是让该基类遍历它所具有的所有tableadapters并更新其连接字符串,而无需任何人向该表单添加任何代码。 They just put the tableadapters on the form and don't worry about the connection string settings as it's all handled in the base class. 他们只是将tableadapters放在窗体上,而不必担心连接字符串设置,因为它们都是在基类中处理的。

The problem I'm having is my reflection code can find the property fine but can't set it. 我遇到的问题是我的反射代码可以找到合适的属性,但是无法设置它。 Can someone help? 有人可以帮忙吗?

Below is code (updated) 下面是代码(已更新)

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

EDIT: 编辑:

When you do SetValue , you need to pass in the object that you wish to set the property on. 当您执行SetValue ,您需要传入要设置其属性的对象。

  • In your first example code, you passed in ItemComp : This is incorrect, since the ConnectionString is a property of the SqlConnection which is a property of ItemComp 在您的第一个示例代码中,您传入了ItemComp :这是不正确的,因为ConnectionStringSqlConnection的属性,而SqlConnectionItemComp的属性
  • In your edited question (and my original answer) you pass in the TASqlConnection . 在您编辑的问题(和我的原始答案)中,您传入TASqlConnection However, this is not the object, but a PropertyInfo based of the object 但是,这不是对象,而是基于对象的PropertyInfo
  • The correct way is to get the value from the ItemComp object and pass that in: 正确的方法是从ItemComp对象获取值并将其传递给:

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

ORIGINAL (INCORRECT) ANSWER: 原始(错误)答案:

You're trying to set a ConnectionString property of ItemComp . 您正在尝试设置ItemCompConnectionString属性。 The ConnectionString is not a property of the TableAdapter but of the SqlConnection (which is a property of the TableAdapter ). ConnectionString不是TableAdapter的属性,而是SqlConnection的属性(这是TableAdapter的属性)。

The correct way of setting the property would be this: 设置属性的正确方法是:

property.SetValue(TASQLConnection, ConvertedProperty, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM