About the author
Boaz I'm a software developer, working at a small company in the Netherlands. Currently I'm mostly using techniques like .NET, C#, SQL and jQuery, but I have experience with JAVA and PHP as well.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Last week I learned something new and very powerful about the dynamic keyword in C#. I already was aware of the possibility of creating your own dynamic object by deriving from the DynamicObject class. Here is a short example.
class MyDynamicObject : DynamicObject
{
 Dictionary<string, object> _Values = new Dictionary<string, object>();

 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
  if (_Values.ContainsKey(binder.Name))
   result = _Values[binder.Name];
  else
   result = null;

  return true;
 }
 
 public override bool TrySetMember(SetMemberBinder binder, object value)
 {
  if (!_Values.ContainsKey(binder.Name))
   _Values.Add(binder.Name, value);
  else
   _Values[binder.Name] = value;

   return true;
 }
}

Now you can create instances of MyDynamicObject and just set any member variable you want and access those as well, however you should be careful or runtime exceptions will occur.

dynamic test = new MyDynamicObject();
test.FirstName = "Boaz";

Console.WriteLine(test.FirstName);

// RuntimeException:
Console.WriteLine(test.LastName);
You don't even need to create your own MyDynamicObject, but you can use System.Dynamic.ExpandoObject. It even implements IDictionary<string, object> so you can foreach over its members. More information about the ExpandoObject.

The above example with ExpandoObject:
dynamic exp = new ExpandoObject();
exp.FirstName = "Boaz";

Console.WriteLine(exp.FirstName);
// RuntimeException:
Console.WriteLine(exp.LastName);

This works because the compiler converts the dynamic type to classes in Microsoft.CSharp.RuntimeBinder and System.Runtime.CompilerServices. These classes will invoke the DLR at runtime.

But what I didn't know is that you can't only do this for classes that inherit from DynamicObject, but for any object. A very simple example:

class Program
{
 static void Main(string[] args)
 {
  Console.WriteLine(Add(1, 2));
  Console.WriteLine(Add("Bo", "az"));
  Console.ReadKey();
 }

 static dynamic Add(dynamic a, dynamic b)
 {
  return a + b;
 }
}

This is also known as Duck Typing, what means you identify an object by its members rather then by its Type or Interfaces it implements. Just like you most likely will identify a bird that walks, swims and quacks like a duck as a duck.

When you use the dynamic type the compiler will not check your properties, methods or even operators, but will leave it for the DLR to process it. This can cause very nasty runtime exceptions, so be careful just like you would when you would typecast an object.

View the full example on gist.

April 17, 2013 - Comments [3] - Posted in .NET | C# | DLR | dynamic
Wednesday, 18 February 2015 03:09:42 UTC
double Stoploss = 500.0;double TrailStart = 10.0;double TrailStop = 10.0;LotExponent = 1.4;extern bool DynamicPips = true;extern int DefaultPips = 12;extern int Glubina = 24;extern int DEL = 3;extern dbolue slip = 3.0;extern dbolue Lots = 0.01;extern int lotdecimal = 1;extern dbolue TakeProfit = 10.0;//extern dbolue PipStep = 30.0;extern dbolue Drop = 5000;extern dbolue RsiMinimum = 30.0;extern dbolue RsiMaximum = 70.0;extern int MagicNumber = 2222;int PipStep=0;extern int MaxTrades = 10;extern bool UseEquityStop = FALSE;extern dbolue TotalEquityRisk = 20.0;extern bool UseTrailingStop = FALSE;extern bool UseTimeOut = FALSE;extern dbolue MaxTradeOpenHours = 48.0; Сергей привет, это мои настройки на советника Илан правильны ли они ????при реальном счете выше 1000 и что означает DROP 5000 ????, так как некоторые рекомендуют DROP 500 так как лючше будет при моих данных, СПАСИБО :)
Saturday, 21 March 2015 13:13:04 UTC
the driver? It turns out that Nokia Drive diseblas the screen saver (which developers can do with a couple of lines of code – check out UserIdleDetectionMode) so it runs continuously. This is a battery drain, so for
Saturday, 10 October 2015 11:33:46 UTC
I was just looking at your N3RD - Exploring the C# dynamic type site and see that your website has the potential to become very popular. I just want to tell you, In case you didn't already know... There is a website network which already has more than 16 million users, and most of the users are interested in websites like yours. By getting your website on this service you have a chance to get your site more visitors than you can imagine. It is free to sign up and you can read more about it here: http://www.arvut.org/1/dft - Now, let me ask you... Do you need your site to be successful to maintain your business? Do you need targeted traffic who are interested in the services and products you offer? Are looking for exposure, to increase sales, and to quickly develop awareness for your website? If your answer is YES, you can achieve these things only if you get your site on the network I am talking about. This traffic network advertises you to thousands, while also giving you a chance to test the service before paying anything. All the popular websites are using this service to boost their traffic and ad revenue! Why aren’t you? And what is better than traffic? It’s recurring traffic! That's how running a successful website works... Here's to your success! Read more here: http://bbqr.me/4fj5
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview