4

I'm having a general class in that class i've written one method which should accept the object class's object as parameter.

the function is as follows-

protected void AddNewForm(object o )
{
    try
    {
        o.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

and i'm calling this function from another classes . but when i call this function as-

Contact objContact=new Contact(); 
AddNewForm(objContact);

but it shows the error in that function. the error as-

'object' does not contain a definition for 'Show' and no extension method 'Show' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

How to implement late binding in C# windows application?

thanks.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
Priyanka
  • 2,802
  • 14
  • 55
  • 88

2 Answers2

7

If you use .NET 4 you can use the new dynamic keyword:

protected void AddNewForm(dynamic o)
{
    try
    {
        o.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

If you don't use .NET 4 you will have to resort to reflection.
It then would look something like this:

protected void AddNewForm(object o)
{
    try
    {
        o.GetType().GetMethod("Show", new Type[0]).Invoke(o, null);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

But you really should consider using a common interface:

interface IShowable
{
    void Show();
}

class Contact : IShowable
{
    public void Show() { /* ... */ }
}

protected void AddNewForm(IShowable o)
{
    try
    {
        o.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • @Daniel Hilgarth : I'm using .NET 4, depending on your reply i used first code but it gives the following error `Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference to System.Core.dll?` and second error is -`One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?` Controls\frmBaseListView.cs 198 17 MSGBL ` – Priyanka May 27 '11 at 12:57
  • @Daniel Hilgarth : also i worked on second code then also it show error at runtime as --- `Ambiguous match found` – Priyanka May 27 '11 at 12:57
  • @Richa: And... Did you reference the mentioned DLLs in your project?! – Daniel Hilgarth May 27 '11 at 13:01
  • @Daniel Hilgarth : Thank you very much.. your second code works fine in my appication.. and i want to say thank to Stack overflow owner for such wonderful site.... – Priyanka May 28 '11 at 06:28
0

For anyone still looking for this kind of thing I found https://andy.edinborough.org/Use-Late-Binding-in-C-Now-without-NET-4-0 very helpful, I will not post the whole contents here, but it is also available at wayback machine if it ever goes stale

NiKiZe
  • 1,256
  • 10
  • 26