Add a variable to a class instance at runtime: Difference between revisions

Content added Content deleted
(Added C# implementation.)
Line 151: Line 151:
aMember contains: A value
aMember contains: A value
anotherMember contains: some other value</lang>
anotherMember contains: some other value</lang>

=={{header|C sharp|C#}}==
{{works with|C sharp|C#|4.0}}
<lang csharp>// ----------------------------------------------------------------------------------------------
//
// Program.cs - DynamicClassVariable
//
// Mikko Puonti, 2013
//
// ----------------------------------------------------------------------------------------------

using System;
using System.Dynamic;

namespace DynamicClassVariable
{
internal static class Program
{
#region Static Members

private static void Main()
{
// To enable late binding, we must use dynamic keyword
// ExpandoObject readily implements IDynamicMetaObjectProvider which allows us to do some dynamic magic
dynamic sampleObj = new ExpandoObject();
// Adding a new property
sampleObj.bar = 1;
Console.WriteLine( "sampleObj.bar = {0}", sampleObj.bar );

// We can also add dynamically methods and events to expando object
// More information: http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx
// This sample only show very small part of dynamic language features - there is lot's more
Console.WriteLine( "< Press any key >" );
Console.ReadKey();
}

#endregion
}
}</lang>

{{out}}
<pre>sampleObj.bar = 1
< Press any key ></pre>



=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==