mercredi 20 mai 2015

Compiler says dynamic property is missing but I can see it

I'm starting to dive into the world of C# Dynamics and Metaprogramming, and having some trouble.

I managed to create a CodeDom tree, and generate the following code:

namespace Mimsy {
    using System;
    using System.Text;
    using System.Collections;

    internal class JubJub {
         private int _wabeCount;
         private ArrayList _updates;

         public JubJub(int wabeCount) {
               this._updates = new ArrayList();
               this.WabeCount = wabeCount;
         }

         public int WabeCount {
               get {
                   return this._wabeCount;
               }
               set {
                   if((value < 0))
                        this._wabeCount = 0;
                   else
                        this._wabeCount = value;
                   this._updates.Add(this._wabeCount);
               }
         }

         public string GetWabeCountHistory() {
               StringBuilder result = new StringBuilder();
               int ndx;
               for(ndx = 0; (ndx < this._updates.Count); ndx = ndx + 1) {
                     if((ndx == 0))
                            result.AppendFormat("{0}", this._updates[ndx]);
                     else
                            result.AppendFormat(", {0}", this._updates[ndx]);
               }
         }
    }
}

I am then compiling dynamically this namespace to an assembly named "dummy".

I can succesfully get an instance of this Type:

string typeName = "Mimsy.JubJub";
Type type = dummyAssembly.GetType(typeName);
dynamic obj = Activator.CreateInstance(type, new object[] { 8 });
//obj is a valid instance type

If I debug this code, I can see in the debugger that obj actually has the property WabeCount:

Debugger Information

However, when trying to access this property, the compiler shouts that the dynamic property does not exist.

Debugger Information 2





Aucun commentaire:

Enregistrer un commentaire