Implementing Dynamic Objects in C# 5.0

Article translations Article translations
Article ID: 2791800 - View products that this article applies to.
Expand all | Collapse all

On This Page

About Author:

Collapse this tableExpand this table
Collapse this imageExpand this image
MVP
This article is provided by Sanchez Roman Canlas, a Senior Developer Consultant at US Technologies Global and a core member of the Phillipine Windows Developer Group

Implementing Dynamic Objects

By simply subclassing DynamicObject class an object can provide binding semantics used for dynamic binding. A DynamicObject class provides an overridable default implementation.

What To Try

using System;
using System.Dynamic;

static void Main()
{
dynamic person = new Person();
person.DynamicMethod(); // the overidden TryInvokeMember method below allows the dynamic object person to invoke a method
}


public class Person : DynamicObject
{
public override bool TryInvokeMember (InvokeMemberBinder binder, object[] args, out object result)
{
Console.Writeline(binder.Name + " method invoked");
result = null;
return true;
}
}

References

MSDN

http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.tryinvokemember(v=vs.100).aspx


Community Solutions Content Disclaimer

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Properties

Article ID: 2791800 - Last Review: September 25, 2013 - Revision: 1.1
Applies to
  • Microsoft Visual C# 2010 Express
Keywords: 
kbcommunity kbstepbystep kbmvp KB2791800

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com