Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using Microsoft.CSharp.RuntimeBinder;
  6. using System.Runtime.CompilerServices;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10. class Program
  11. {
  12.  
  13. static void Main()
  14. {
  15. var c = new DbColumn() { ColumnName = "test" };
  16.  
  17. var sw = Stopwatch.StartNew();
  18. for (var i = 0; i < 10000000; i++)
  19. {
  20. var test = c["ColumnName"];
  21. }
  22. sw.Stop();
  23. Console.WriteLine(sw.ElapsedMilliseconds);
  24. }
  25. }
  26.  
  27. public class DbColumn
  28. {
  29. public virtual bool AllowDBNull { get; set; }
  30. public virtual string BaseCatalogName { get; set; }
  31. public virtual string BaseColumnName { get; set; }
  32. public virtual string BaseSchemaName { get; set; }
  33. public virtual string BaseServerName { get; set; }
  34. public virtual string BaseTableName { get; set; }
  35. public virtual string ColumnName { get; set; }
  36.  
  37. public virtual string GetColumnName() { return ColumnName; }
  38.  
  39. public virtual int ColumnOrdinal { get; set; }
  40. public virtual int ColumnSize { get; set; }
  41. public virtual bool IsAliased { get; set; }
  42. public virtual bool IsAutoIncrement { get; set; }
  43. public virtual bool IsExpression { get; set; }
  44. public virtual bool IsHidden { get; set; }
  45. public virtual bool IsIdentity { get; set; }
  46. public virtual bool IsKey { get; set; }
  47. public virtual bool IsLong { get; set; }
  48. public virtual bool IsReadOnly { get; set; }
  49. public virtual bool IsUnique { get; set; }
  50. public virtual int NumericPrecision { get; set; }
  51. public virtual int NumericScale { get; set; }
  52. public virtual string UdtAssemblyQualifiedName { get; set; }
  53. public virtual Type DataType { get; set; }
  54. public virtual string DataTypeName { get; set; }
  55. #if USING_SWITCH
  56. public virtual object this[string property]
  57. {
  58. get
  59. {
  60. switch (property)
  61. {
  62. case "AllowDBNull":
  63. return AllowDBNull;
  64. case "BaseCatalogName":
  65. return BaseCatalogName;
  66. case "BaseColumnName":
  67. return BaseColumnName;
  68. case "BaseSchemaName":
  69. return BaseSchemaName;
  70. case "BaseServerName":
  71. return BaseServerName;
  72. case "BaseTableName":
  73. return BaseTableName;
  74. case "ColumnName":
  75. return ColumnName;
  76. case "ColumnOrdinal":
  77. return ColumnOrdinal;
  78. case "ColumnSize":
  79. return ColumnSize;
  80. case "IsAliased":
  81. return IsAliased;
  82. case "IsAutoIncrement":
  83. return IsAutoIncrement;
  84. case "IsExpression":
  85. return IsExpression;
  86. case "IsHidden":
  87. return IsHidden;
  88. case "IsIdentity":
  89. return IsIdentity;
  90. case "IsKey":
  91. return IsKey;
  92. case "IsLong":
  93. return IsLong;
  94. case "IsReadOnly":
  95. return IsReadOnly;
  96. case "IsUnique":
  97. return IsUnique;
  98. case "NumericPrecision":
  99. return NumericPrecision;
  100. case "NumericScale":
  101. return NumericScale;
  102. case "UdtAssemblyQualifiedName":
  103. return UdtAssemblyQualifiedName;
  104. case "DataType":
  105. return DataType;
  106. case "DataTypeName":
  107. return DataTypeName;
  108. default:
  109. return null;
  110. }
  111. }
  112. }
  113. #else
  114. static Dictionary<string,CallSite<Func<CallSite, object, object>>> _dictionary = new Dictionary<string, CallSite<Func<CallSite, object, object>>>();
  115. static DbColumn()
  116. {
  117. foreach(var prop in typeof(DbColumn).GetProperties(BindingFlags.Instance | BindingFlags.Public))
  118. {
  119. var binder = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, prop.Name, typeof(DbColumn),
  120. new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
  121.  
  122. _dictionary.Add(prop.Name, CallSite<Func<CallSite, object, object>>.Create(binder));
  123. }
  124.  
  125. }
  126.  
  127. public virtual object this[string propertyName]
  128. {
  129. get
  130. {
  131. var callsite = _dictionary[propertyName];
  132. return callsite.Target(callsite, this);
  133. }
  134. }
  135. #endif
  136. }
  137.  
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement