C# 反射.设置 TableAdapter ConnectionString [英] C# Reflection. Set TableAdapter ConnectionString

查看:54
本文介绍了C# 反射.设置 TableAdapter ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助解决这个问题.我一直在尝试为 WinForm 创建一个新的基类.我想要做的是让这个基类遍历它上面的所有 tableadapters 并更新它们的连接字符串,而无需任何人向表单添加任何代码.他们只是把表格适配器放在表单上,​​不用担心连接字符串的设置,因为它都是在基类中处理的.

I hope someone can help with this one. I've been trying to create a new base class for a 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. 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.

我遇到的问题是我的反射代码可以很好地找到该属性,但无法设置它.有人可以帮忙吗?

The problem I'm having is my reflection code can find the property fine but can't set it. Can someone help?

下面是代码(更新)

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 时,您需要传入您希望为其设置属性的对象.

When you do SetValue, you need to pass in the object that you wish to set the property on.

  • 在您的第一个示例代码中,您传入了 ItemComp:这是不正确的,因为 ConnectionStringSqlConnection 的一个属性,它是ItemComp
  • 的一个属性
  • 在您编辑的问题(以及我的原始答案)中,您传入了 TASqlConnection.然而,这不是对象,而是基于对象的PropertyInfo
  • 正确的方法是从 ItemComp 对象中获取值并传入:
  • 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
  • In your edited question (and my original answer) you pass in the TASqlConnection. However, this is not the object, but a PropertyInfobased of the object
  • The correct way is to get the value from the ItemComp object and pass that in:

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

原始(不正确)答案:

您正在尝试设置 ItemCompConnectionString 属性.ConnectionString 不是 TableAdapter 的属性,而是 SqlConnection 的属性(它是 TableAdapter 的属性).

You're trying to set a ConnectionString property of ItemComp. The ConnectionString is not a property of the TableAdapter but of the SqlConnection (which is a property of the TableAdapter).

设置属性的正确方法是:

The correct way of setting the property would be this:

property.SetValue(TASQLConnection, ConvertedProperty, null);

这篇关于C# 反射.设置 TableAdapter ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆