none
反射的代码的问题。输出结果与预期结果不致,贴上原码,谢谢。 RRS feed

  • 问题

  • [C#]
     class Program
     {
      static void Main(string[] args)
      {
       Type typeXX = typeof(string);
    
       AssemblyName an = new AssemblyName();
       an.Name = "xxx";
       AppDomain domain = System.Threading.Thread.GetDomain();
    
       AssemblyBuilder ab = domain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
       ModuleBuilder mb = ab.DefineDynamicModule("xxx", "xxx.dll");
    
       TypeBuilder tb = mb.DefineType("Test", TypeAttributes.Public | TypeAttributes.Class, typeof(Bland));
    
       var dict = GetDictionary();
    
       //创建构造函数
       CreateConstructor(tb);
    
       //创建属性
       CreateProperts(tb, dict);
    
    
       Type t = tb.CreateType();
    
       ab.Save("xxx.dll");
    
    
       dynamic person = Activator.CreateInstance(t, dict);
    
    
       Console.WriteLine(person.ID);
       Console.WriteLine(person.Name);
       Console.WriteLine(person.Age);
       Console.WriteLine(person.Birthday);
       Console.WriteLine(person.InTime);
    
    
       
    
       Console.Read();
      }
    
    
      private static void CreateProperts(TypeBuilder tb, IDictionary properts)
      {
       foreach (DictionaryEntry p in properts)
       {
        //创建属性
        CreatePropert(tb, p);
       }
      }
    
      private static void CreatePropert(TypeBuilder tb, DictionaryEntry p)
      {
       Type type = p.Value.GetType();
    
       var p1 = tb.DefineProperty(p.Key.ToString(), PropertyAttributes.HasDefault,type, null);
    
       p1.SetGetMethod(CreateGetPropert(tb, p));
       p1.SetSetMethod(CreateSetPropert(tb, p));
    
      }
    
    
      private static MethodBuilder CreateGetPropert(TypeBuilder tb, DictionaryEntry p)
      {
       var get = tb.DefineMethod("get_" + p.Key, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName
          , p.Value.GetType(), null);
    
       var m1 = typeof(Bland).GetMethod("GetItem");
    
       var gen = get.GetILGenerator();
    
       gen.Emit(OpCodes.Ldarg_0);
       gen.Emit(OpCodes.Ldstr, p.Key.ToString());
       gen.Emit(OpCodes.Call, m1);
       gen.Emit(OpCodes.Ret);
    
       return get;
      }
    
    
      private static MethodBuilder CreateSetPropert(TypeBuilder tb, DictionaryEntry p)
      {
       var get = tb.DefineMethod("set_" + p.Key, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName
          , null, new Type[] { p.Value.GetType() });
    
       var m1 = typeof(Bland).GetMethod("SetItem");
    
       var gen = get.GetILGenerator();
    
       gen.Emit(OpCodes.Ldarg_0);
       gen.Emit(OpCodes.Ldstr, p.Key.ToString());
       gen.Emit(OpCodes.Ldarg_1);
       gen.Emit(OpCodes.Call, m1);
       gen.Emit(OpCodes.Ret);
    
       return get;
      }
    
    
      private static void CreateConstructor(TypeBuilder tb)
      {
       var cb = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
        CallingConventions.Standard, new Type[] { typeof(IDictionary) });
    
       ConstructorInfo ctor1 = typeof(Bland).GetConstructor(new Type[] { typeof(IDictionary) });
    
    
       var gen = cb.GetILGenerator();
    
      
    
       gen.Emit(OpCodes.Ldarg_0);
       gen.Emit(OpCodes.Ldarg_1);
       gen.Emit(OpCodes.Call, ctor1);
       
       gen.Emit(OpCodes.Ret);
      }
    
    
      static IDictionary GetDictionary()
      {
       Dictionary<string, object> dict = new Dictionary<string, object>();
    
       dict.Add("Name", "王一");
       dict.Add("Age", 10);
       dict.Add("ID", 1);
       dict.Add("Birthday", new DateTime(2008, 10, 10));
       dict.Add("InTime", new DateTime(2011, 1, 1));
    
       return dict;
      }
     }
    
    public class Bland
     {
      #region 构造函数
      public Bland(IDictionary row)
      {
       _row = row;
      }
      #endregion
    
      #region 字段
      private IDictionary _row;
      #endregion
    
      #region 公有方法
      public object GetItem(string column)
      {
       return _row[column];
      }
    
    
    
    
      public void SetItem(string column, object value)
      {
       _row[column] = value;
      }
      #endregion
    
     }
    
    [/C#]
    

    实际输出时,动态产生的属性,字符型结果正常,日期型与数字型输出错误。求解答。

    • 已编辑 学习.net 2011年4月21日 9:19 标题错误
    2011年4月21日 9:02

全部回复