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(typeo f(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. 2 1411
The example 3 that you showed is using typeof(Apex.Data.Class1) instead of
GetType(className). Can I assume that the example #3 that you show works?
Now, if you replace the typeof(...) with GetType(className), the reason this
is likely not working is that you're not providing the necessary assembly
information so the class loader can find the type. Here's an excerpt from
the MSDN documentation on GetType(string typeName):
"typeName can be a simple type name, a type name that includes a namespace,
or a complex name that includes an assembly name specification.
If typeName includes only the name of the Type, this method searches in the
calling object's assembly, then in the mscorlib.dll assembly. If typeName is
fully qualified with the partial or complete assembly name, this method
searches in the specified assembly."
You need to provide a fully qualified class name, which looks like this:
MyNamespace.MyClass,MyAssembly.
Ken
"Todd" <tp****@apexcg.com> wrote in message
news:47**************************@posting.google.c om... 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(typeo f(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.
Thanks Ken!
That worked, especially after I got the method name correct in my
code! :~|
I put a second textbox on my form in which I enter the assembly name.
Here's the code now, which uses a different constructor for the
CreateDelegate() method than I was using before only because the IL is
slightly leaner with this code:
string functionName = this.textBox1.Text;
string className = functionName.Substring(0,functionName.LastIndexOf( "."));
string methodName = functionName.Substring(functionName.LastIndexOf(". ")+1);
string assemblyName = this.textBox2.Text;
DataSet ds = ((DataProvider)System.Delegate.CreateDelegate(type of(DataProvider),
Assembly.Load(assemblyName).GetType(className).Get Method(methodName)))();
this.dataGrid1.DataSource = ds.Tables[0];
"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:<#6**************@TK2MSFTNGP09.phx.gbl>... The example 3 that you showed is using typeof(Apex.Data.Class1) instead of GetType(className). Can I assume that the example #3 that you show works?
Now, if you replace the typeof(...) with GetType(className), the reason this is likely not working is that you're not providing the necessary assembly information so the class loader can find the type. Here's an excerpt from the MSDN documentation on GetType(string typeName):
"typeName can be a simple type name, a type name that includes a namespace, or a complex name that includes an assembly name specification. If typeName includes only the name of the Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly."
You need to provide a fully qualified class name, which looks like this: MyNamespace.MyClass,MyAssembly.
Ken
"Todd" <tp****@apexcg.com> wrote in message news:47**************************@posting.google.c om... 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(typeo f(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: LP |
last post by:
Hello!
I am still transitioning from VB.NET to C#. I undertand the basic concepts
of Delegates, more so of Events and somewhat understand AsyncCallback
methods. But I need some clarification on...
|
by: Lior Amar |
last post by:
Quick question about threads and delegates.
I have the following scenario
Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a
callback
Thread B Invokes a DelegateB...
|
by: Nicky Smith |
last post by:
Hello,
I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the
MS press Mcad book for the same topic ".. Windows based applications
with VB.net" for exam 70-306.
In the...
|
by: Viet |
last post by:
I have a couple of questions that hopefully someone could clarify for me. I
have an app that uses the threading.timer to constantly poll a scanner to
scan in documents. I understand that Async...
|
by: GVN |
last post by:
Hi All,
I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:
I have a delegate myDelegate with object dlgt, and two methods...
|
by: =?Utf-8?B?Sko=?= |
last post by:
I have a logger component that logs to multiple sources, ie textfile,
eventlog etc.
and I have two methods that depending on where I call up my logger comp. one
of them will be called. For ex. if...
|
by: bharathreddy |
last post by:
Delegates
Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
|
by: =?Utf-8?B?T2xkQ2FEb2c=?= |
last post by:
My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It’s an object created by a
single line of code by the system and that makes...
|
by: raylopez99 |
last post by:
Hello all—
I’m trying to get the below to work and cannot get the format right.
It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx
What it is: I’m trying...
|
by: samadams_2006 |
last post by:
Hello,
I'm trying to figure out why delegates are such a great thing. For
example, I came across the following line of code:
"This ability to refer to a method as a parameter makes delegates...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |