T
Todd
I am desperately trying to figure out the following problem,
simplified for discussion.
I have a WinForm with a button, textbox, and datagrid. When I click
the button, I want to execute the method defined in the textbox, which
must return a dataset, and pass this dataset to the datasource of my
datagrid. Delegates and/or Reflection seem to be the natural way to
accomplish this.
My "Data tier" resides in another assembly which is referenced in my
WinForms project. I have in that assembly the following class
definition:
namespace Apex.Data
{
public class Class1
{
private Class1(){}
public static DataSet GetDataSet()
{
//return my dataset here...
}
}
}
So, in my attempts to figure this out, I've created the following
delegate and event handler for my button's click event:
public delegate DataSet DataProvider();
private void button1_Click(object sender, System.EventArgs e)
{
//1. manual binding
//this.dataGrid1.DataSource =
Apex.Data.Class1.GetDataSet().Tables[0];
//2. explicitly defined delegate
//DataProvider method = new
DataProvider(Apex.Data.Class1.GetDataSet);
//DataSet ds = method();
//this.dataGrid1.DataSource = ds.Tables[0];
//3. dynamically defined delegate
string functionName = this.textBox1.Text;
string className = functionName.Substring(0,functionName.LastIndexOf("."));
string methodName =
functionName.Substring(functionName.LastIndexOf(".")+1);
DataProvider method =
(DataProvider)System.Delegate.CreateDelegate(typeof(DataProvider),
typeof(Apex.Data.Class1), functionName);
DataSet ds = method();
this.dataGrid1.DataSource = ds.Tables[0];
}
The results of these 3 attempts:
1. I was just making sure the dang static method itself works. It
does.
2. Define the delegate explicitly. No problem.
3. Create the delegate at runtime. NOT HAPPENING.
The problem with #3 appears to be that System.Type.GetType(className)
returns null. Why does it return null? Is there a better approach to
this whole problem? Thanks in advance.
simplified for discussion.
I have a WinForm with a button, textbox, and datagrid. When I click
the button, I want to execute the method defined in the textbox, which
must return a dataset, and pass this dataset to the datasource of my
datagrid. Delegates and/or Reflection seem to be the natural way to
accomplish this.
My "Data tier" resides in another assembly which is referenced in my
WinForms project. I have in that assembly the following class
definition:
namespace Apex.Data
{
public class Class1
{
private Class1(){}
public static DataSet GetDataSet()
{
//return my dataset here...
}
}
}
So, in my attempts to figure this out, I've created the following
delegate and event handler for my button's click event:
public delegate DataSet DataProvider();
private void button1_Click(object sender, System.EventArgs e)
{
//1. manual binding
//this.dataGrid1.DataSource =
Apex.Data.Class1.GetDataSet().Tables[0];
//2. explicitly defined delegate
//DataProvider method = new
DataProvider(Apex.Data.Class1.GetDataSet);
//DataSet ds = method();
//this.dataGrid1.DataSource = ds.Tables[0];
//3. dynamically defined delegate
string functionName = this.textBox1.Text;
string className = functionName.Substring(0,functionName.LastIndexOf("."));
string methodName =
functionName.Substring(functionName.LastIndexOf(".")+1);
DataProvider method =
(DataProvider)System.Delegate.CreateDelegate(typeof(DataProvider),
typeof(Apex.Data.Class1), functionName);
DataSet ds = method();
this.dataGrid1.DataSource = ds.Tables[0];
}
The results of these 3 attempts:
1. I was just making sure the dang static method itself works. It
does.
2. Define the delegate explicitly. No problem.
3. Create the delegate at runtime. NOT HAPPENING.
The problem with #3 appears to be that System.Type.GetType(className)
returns null. Why does it return null? Is there a better approach to
this whole problem? Thanks in advance.