【发布时间】:2015-06-25 20:36:28
【问题描述】:
我希望有人可以帮助解决这个问题。我一直在尝试为 WinForm 创建一个新的基类。我想做的是让这个基类遍历它上面的所有表格适配器并更新它们的连接字符串,而无需任何人向表单添加任何代码。他们只是将表格适配器放在表单上,不用担心连接字符串设置,因为它们都在基类中处理。
我遇到的问题是我的反射代码可以找到该属性但无法设置它。有人可以帮忙吗?
以下是代码(更新)
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;
}
【问题讨论】:
-
你不需要在这里反射(或者你的代码对它的预期用途没有任何意义)。
-
据我所知,没有其他方法可以自动完成我想做的事情。
-
只有在 TableAdapter 不共享公共基类时才会这样。可以? MSDN 在这方面比较模糊。
-
即使在这种情况下,您仍然可以通过将已知接口应用于生成的类并将
ItemComp转换为该类来避免反射。 -
TableAdapter 继承自 Component。将接口应用于生成的类将意味着当它重新生成时它会丢失接口吗?
标签: c# winforms reflection tableadapter