Extensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.Mono.Cecil;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.CLR.TypeSystem;
  8. namespace ILRuntime.Reflection
  9. {
  10. static class Extensions
  11. {
  12. public static object CreateInstance(this CustomAttribute attribute, IType at, Runtime.Enviorment.AppDomain appdomain)
  13. {
  14. object ins;
  15. List<IType> param = null;
  16. if (at is ILType)
  17. {
  18. var it = (ILType)at;
  19. if (!attribute.HasConstructorArguments)
  20. ins = it.Instantiate(true);
  21. else
  22. {
  23. ins = it.Instantiate(false);
  24. if (param == null)
  25. param = new List<IType>();
  26. param.Clear();
  27. object[] p = new object[attribute.ConstructorArguments.Count];
  28. for (int j = 0; j < attribute.ConstructorArguments.Count; j++)
  29. {
  30. var ca = attribute.ConstructorArguments[j];
  31. param.Add(appdomain.GetType(ca.Type, null, null));
  32. p[j] = ca.Value;
  33. }
  34. var ctor = it.GetConstructor(param);
  35. appdomain.Invoke(ctor, ins, p);
  36. }
  37. if (attribute.HasProperties)
  38. {
  39. object[] p = new object[1];
  40. foreach (var j in attribute.Properties)
  41. {
  42. p[0] = j.Argument.Value;
  43. var setter = it.GetMethod("set_" + j.Name, 1);
  44. appdomain.Invoke(setter, ins, p);
  45. }
  46. }
  47. if(attribute.HasFields)
  48. {
  49. foreach (var j in attribute.Fields)
  50. {
  51. int index;
  52. var field = it.GetField(j.Name, out index);
  53. if (field != null)
  54. ((ILRuntime.Runtime.Intepreter.ILTypeInstance)ins)[index] = j.Argument.Value;
  55. }
  56. }
  57. ins = ((ILRuntime.Runtime.Intepreter.ILTypeInstance)ins).CLRInstance;
  58. }
  59. else
  60. {
  61. param = new List<IType>();
  62. object[] p = null;
  63. if (attribute.HasConstructorArguments)
  64. {
  65. p = new object[attribute.ConstructorArguments.Count];
  66. for (int j = 0; j < attribute.ConstructorArguments.Count; j++)
  67. {
  68. var ca = attribute.ConstructorArguments[j];
  69. param.Add(appdomain.GetType(ca.Type, null, null));
  70. p[j] = ca.Value;
  71. }
  72. }
  73. ins = ((CLRMethod)at.GetConstructor(param)).ConstructorInfo.Invoke(p);
  74. if (attribute.HasProperties)
  75. {
  76. foreach (var j in attribute.Properties)
  77. {
  78. var prop = at.TypeForCLR.GetProperty(j.Name);
  79. if (prop.PropertyType == typeof(Type) && j.Argument.Value != null)
  80. {
  81. var type = appdomain.GetType(j.Argument.Value, null, null);
  82. prop.SetValue(ins, type.TypeForCLR, null);
  83. }
  84. else
  85. prop.SetValue(ins, j.Argument.Value, null);
  86. }
  87. }
  88. if(attribute.HasFields)
  89. {
  90. foreach(var j in attribute.Fields)
  91. {
  92. var field = at.TypeForCLR.GetField(j.Name);
  93. field.SetValue(ins, j.Argument.Value);
  94. }
  95. }
  96. }
  97. return ins;
  98. }
  99. }
  100. }