You are currently offline, waiting for your internet to reconnect

Implementing Dynamic Objects in C# 5.0

About Author:
MVPThis 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;
}
}


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: 09/25/2013 11:08:00 - Revision: 1.1

Microsoft Visual C# 2010 Express

  • kbcommunity kbstepbystep kbmvp KB2791800
Feedback
style="display:none;" onerror="var m=document.createElement('meta');m.name='ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?">