title: C# casting an object within lambda
body: <p>I am currently trying to set a field which I need in business logic which in this case is Lazy.
  (yes not the property, it is necessary to set the field)
  I get the error that Lazy can not be converted to Lazy
  as you can see:</p>
  
  <blockquote>
    <p>Object of type
    'BusinessLogic.Lazy<code>1[System.Object]'
    cannot be converted to type
    'BusinessLogic.Lazy</code>1[BusinessLogic.ArtikelBLL]</p>
  </blockquote>
  
  <p>I use this line to get a dynamic repository.</p>
  
  <pre><code>dynamic repository = Activator.CreateInstance(typeof(GenericRepository&lt;&gt;).MakeGenericType(typeArgs));
  </code></pre>
  
  <p>Then I try to set the value of the field but it fails:</p>
  
  <pre><code>fInfo.SetValue(obj, Lazy.From(() =&gt; repository.GetDataById(id)));
  </code></pre>
  
  <p>I have tried to solve it many different ways.
  Somehow I have to cast <code>repository.GetDataById(id)</code> to the Entity it is looking for, which in this case is ArtikelBLL (which i can get through <code>pInfo.PropertyType</code>).
  But by doing <code>(ArtikelBLL)repository.GetDataById(id)</code> it will not remain object orientated.
  Can anybody please help me with this?</p>