Created
February 9, 2016 01:56
-
-
Save kkurni/eda73290642d56417e75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Reflection; | |
using Microsoft.CSharp.RuntimeBinder; | |
using System.Runtime.CompilerServices; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var c = new DbColumn() { ColumnName = "test" }; | |
var sw = Stopwatch.StartNew(); | |
for (var i = 0; i < 10000000; i++) | |
{ | |
var test = c["ColumnName"]; | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.ElapsedMilliseconds); | |
} | |
} | |
public class DbColumn | |
{ | |
public virtual bool AllowDBNull { get; set; } | |
public virtual string BaseCatalogName { get; set; } | |
public virtual string BaseColumnName { get; set; } | |
public virtual string BaseSchemaName { get; set; } | |
public virtual string BaseServerName { get; set; } | |
public virtual string BaseTableName { get; set; } | |
public virtual string ColumnName { get; set; } | |
public virtual string GetColumnName() { return ColumnName; } | |
public virtual int ColumnOrdinal { get; set; } | |
public virtual int ColumnSize { get; set; } | |
public virtual bool IsAliased { get; set; } | |
public virtual bool IsAutoIncrement { get; set; } | |
public virtual bool IsExpression { get; set; } | |
public virtual bool IsHidden { get; set; } | |
public virtual bool IsIdentity { get; set; } | |
public virtual bool IsKey { get; set; } | |
public virtual bool IsLong { get; set; } | |
public virtual bool IsReadOnly { get; set; } | |
public virtual bool IsUnique { get; set; } | |
public virtual int NumericPrecision { get; set; } | |
public virtual int NumericScale { get; set; } | |
public virtual string UdtAssemblyQualifiedName { get; set; } | |
public virtual Type DataType { get; set; } | |
public virtual string DataTypeName { get; set; } | |
#if USING_SWITCH | |
public virtual object this[string property] | |
{ | |
get | |
{ | |
switch (property) | |
{ | |
case "AllowDBNull": | |
return AllowDBNull; | |
case "BaseCatalogName": | |
return BaseCatalogName; | |
case "BaseColumnName": | |
return BaseColumnName; | |
case "BaseSchemaName": | |
return BaseSchemaName; | |
case "BaseServerName": | |
return BaseServerName; | |
case "BaseTableName": | |
return BaseTableName; | |
case "ColumnName": | |
return ColumnName; | |
case "ColumnOrdinal": | |
return ColumnOrdinal; | |
case "ColumnSize": | |
return ColumnSize; | |
case "IsAliased": | |
return IsAliased; | |
case "IsAutoIncrement": | |
return IsAutoIncrement; | |
case "IsExpression": | |
return IsExpression; | |
case "IsHidden": | |
return IsHidden; | |
case "IsIdentity": | |
return IsIdentity; | |
case "IsKey": | |
return IsKey; | |
case "IsLong": | |
return IsLong; | |
case "IsReadOnly": | |
return IsReadOnly; | |
case "IsUnique": | |
return IsUnique; | |
case "NumericPrecision": | |
return NumericPrecision; | |
case "NumericScale": | |
return NumericScale; | |
case "UdtAssemblyQualifiedName": | |
return UdtAssemblyQualifiedName; | |
case "DataType": | |
return DataType; | |
case "DataTypeName": | |
return DataTypeName; | |
default: | |
return null; | |
} | |
} | |
} | |
#else | |
static Dictionary<string,CallSite<Func<CallSite, object, object>>> _dictionary = new Dictionary<string, CallSite<Func<CallSite, object, object>>>(); | |
static DbColumn() | |
{ | |
foreach(var prop in typeof(DbColumn).GetProperties(BindingFlags.Instance | BindingFlags.Public)) | |
{ | |
var binder = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, prop.Name, typeof(DbColumn), | |
new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); | |
_dictionary.Add(prop.Name, CallSite<Func<CallSite, object, object>>.Create(binder)); | |
} | |
} | |
public virtual object this[string propertyName] | |
{ | |
get | |
{ | |
var callsite = _dictionary[propertyName]; | |
return callsite.Target(callsite, this); | |
} | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment