?
Solved

Delegation passed as parameter in Ctor

Posted on 2005-05-10
5
Medium Priority
?
307 Views
Last Modified: 2010-04-16
i have a question regarding delegation:
i have a windows application and a class library.
i want to pass a delegate object in the ctor of "Class" declared in the test library so when some event occured, the Test library will trigger the event and notify the windows app.

i created new Class Library project with one class:

public delegate void ViewDelegate();   // delegate declaration

public interface IViewDelegate
{
     event ViewDelegate DelegateEvent;
     void FireAway();
}

namespace TestLibrary
{
     public class Class
     {
          private IViewDelegate _viewDelegate;
          public Class(IViewDelegate viewDelegate)
          {
               _viewDelegate = viewDelegate;
          }
     }
}

then created another Window Application project with this code:
namespace TestWindowsApplication
{
     public class Form1 : System.Windows.Forms.Form, IViewDelegate
     {
          private System.ComponentModel.Container components = null;
          public event ViewDelegate DelegateEvent;

          public Form1()
          {
               //
               // Required for Windows Form Designer support
               //
               InitializeComponent();

               DelegateEvent+=new ViewDelegate(MainForm_DelegateEvent);
          }

          private void MainForm_DelegateEvent()
          {

          }

          public void FireAway()
          {
               DelegateEvent();
          }

          private void Form1_Load(object sender, System.EventArgs e)
          {
               string strAsm = "C:\\projects\\TestWindowsApplication\\TestWindowsApplication\\bin\\TestLibrary.dll";
               Assembly asm = Assembly.LoadFile(strAsm);
               Object obj = asm.CreateInstance("TestLibrary.Class", true, BindingFlags.CreateInstance, null, new object[]{(IViewDelegate)this}, null, null);
          }
     }
}

in  Form1_Load i wanna dynamically create and instance of Class from the TestLibrary dll.
i double checked the path of the library but still the exception is being thrown saying Member not found.
btw, if i change ctor declaration to accept primitive types such as integer/string it work fine.

what's the matter?

 
Comment from sedgwick
Date: 05/10/2005 01:11AM CST
 Your Comment  


if i explicitly add the TestLibrary.dll to the TestWindowsApplication project and do the following:
TestLibrary.Class c = new TestLibrary.Class(this);

it works ok.

which means that something in the CreateInstance() invokation is wrong, isnt it?

also, if i change the ctor in the TestLibrary to accept primitive types such as string or integer it works ok.

how come?
0
Comment
Question by:Meir Rivkin
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13966426
to use delegates with events, you must have the following signature:
1. It returns void
2. There are two parameters: the first must derive from or be an object, the second must derive from or be an EventArgs

Thus, since you have nothing special in your delegate, you could use the EventHandler
public interface IViewDelegate
{
      event EventHandler DelegateEvent;
      void FireAway();
}

namespace TestWindowsApplication
{
      public class Form1 : System.Windows.Forms.Form, IViewDelegate
      {
            private System.ComponentModel.Container components = null;
            public event EventHandler DelegateEvent;
            
            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
                  
                  DelegateEvent+=new ViewDelegate(MainForm_DelegateEvent);
            }
            
            private void MainForm_DelegateEvent(object sender, EventArgs e)
            {
                  
            }
            
            public void FireAway()
            {
                  OnDelegateEvent();
            }
            
            protected void OnDelegateEvent()
            {
                  if (DelegateEvent != null)
                        DelegateEvent(this,EventArgs.Empty);
            }
            
            private void Form1_Load(object sender, System.EventArgs e)
            {
                  string strAsm = "C:\\projects\\TestWindowsApplication\\TestWindowsApplication\\bin\\TestLibrary.dll";
                  Assembly asm = Assembly.LoadFile(strAsm);
                  Object obj = asm.CreateInstance("TestLibrary.Class", true, BindingFlags.CreateInstance, null, new object[]{(IViewDelegate)this}, null, null);
            }
      }
}

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13966435
DelegateEvent+=new EventHandler(MainForm_DelegateEvent);

and so on and so forth. If you use delegates with references to Events, then you can alter the signature as you wish
0
 
LVL 42

Author Comment

by:Meir Rivkin
ID: 13966758
hi b1xml2.  10x for the quick reply.

unfortunately it doesn;t work, did u test the code?

it still throws an exception of "Member not found"
0
 
LVL 42

Author Comment

by:Meir Rivkin
ID: 13966808
i found a way to overcome this problem buy wrapping the interface IViewDelegate in a seperate dll instead of explicitly declare it in the projects.
then when i add reference the dll the CreateInstance works fine.

still i wanna know why it doesn't work as is.
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13966827

public interface IViewDelegate
{
     event EventHandler DelegateEvent;
     void FireAway();
}

namespace TestLibrary
{
     public class Class
     {
          private IViewDelegate viewDelegate;
          public Class(IViewDelegate viewDelegate)
          {
               this.viewDelegate = viewDelegate;
          }
     }
}

then created another Window Application project with this code:
namespace TestWindowsApplication
{
     public class Form1 : System.Windows.Forms.Form, IViewDelegate
     {
          private System.ComponentModel.Container components = null;
          public event EventHandler DelegateEvent;

          public Form1()
          {
               //
               // Required for Windows Form Designer support
               //
               InitializeComponent();

               DelegateEvent+=new EventHandler(MainForm_DelegateEvent);
          }

          private void MainForm_DelegateEvent(object sender,EventArgs e)
          {

          }

          public void FireAway()
          {
              if (this.DelegateEvent != null)
                  this.DelegateEvent(this,EventArgs.Empty);
          }

          private void Form1_Load(object sender, System.EventArgs e)
          {
               string path = @"C:\projects\TestWindowsApplication\TestWindowsApplication\bin\TestLibrary.dll";
               Assembly assembly = Assembly.LoadFile(path);
             IViewDelegate viewDelegate = this;
               object value = assembly.CreateInstance("TestLibrary.Class", true, BindingFlags.CreateInstance, null, new object[]{viewDelegate}, null, null);
          }
     }
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question