public static void Test()
{
int LOOP_LENGTH = 100000000;
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("doesPropertyExist");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.doesPropertyExist(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.doesPropertyExist(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("HasProperty");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.HasProperty(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.HasProperty(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("IsPropertyExist");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.IsPropertyExist(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.IsPropertyExist(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("IsPropertyExistBinderException");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.IsPropertyExistBinderException(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.IsPropertyExistBinderException(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("PropertyExists");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.PropertyExists(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.PropertyExists(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
{
long first_memory = GC.GetTotalMemory(true);
var stopWatch = Stopwatch.StartNew();
Console.WriteLine("PropertyExistsJToken");
dynamic testdo = new { A = 1, B = (string)null, C = "A" };
for (int i = 0; i < LOOP_LENGTH; i++)
{
if (!TestDynamic.PropertyExistsJToken(testdo, "A"))
{
Console.WriteLine("throw find");
break;
}
if (TestDynamic.PropertyExistsJToken(testdo, "ABC"))
{
Console.WriteLine("throw not find");
break;
}
}
stopWatch.Stop();
var last_memory = GC.GetTotalMemory(true);
Console.WriteLine($" Time:{stopWatch.Elapsed.TotalSeconds}s\t Memory:{last_memory - first_memory}");
}
}
public static bool IsPropertyExist(dynamic settings, string name)
{
if (settings is ExpandoObject)
return ((IDictionary<string, object>)settings).ContainsKey(name);
return settings.GetType().GetProperty(name) != null;
}
public static bool HasProperty(dynamic obj, string name)
{
Type objType = obj.GetType();
if (objType == typeof(ExpandoObject))
{
return ((IDictionary<string, object>)obj).ContainsKey(name);
}
return objType.GetProperty(name) != null;
}
public static bool PropertyExists(dynamic obj, string name)
{
if (obj == null) return false;
if (obj is IDictionary<string, object> dict)
{
return dict.ContainsKey(name);
}
return obj.GetType().GetProperty(name) != null;
}
// public static bool HasPropertyExist(dynamic settings, string name)
// {
// if (settings is System.Dynamic.ExpandoObject)
// return ((IDictionary<string, object>)settings).ContainsKey(name);
// if (settings is DynamicJsonObject)
// try
// {
// return settings[name] != null;
// }
// catch (KeyNotFoundException)
// {
// return false;
// }
// return settings.GetType().GetProperty(name) != null;
// }
public static bool IsPropertyExistBinderException(dynamic dynamicObj, string property)
{
try
{
var value = dynamicObj[property].Value;
return true;
}
catch (RuntimeBinderException)
{
return false;
}
}
public static bool HasPropertyFoundException(dynamic obj, string name)
{
try
{
var value = obj[name];
return true;
}
catch (KeyNotFoundException)
{
return false;
}
}
public static bool doesPropertyExist(dynamic obj, string property)
{
return ((Type)obj.GetType()).GetProperties().Where(p => p.Name.Equals(property)).Any();
}
public static bool PropertyExistsJToken(dynamic obj, string name)
{
if (obj == null) return false;
if (obj is ExpandoObject)
return ((IDictionary<string, object>)obj).ContainsKey(name);
if (obj is IDictionary<string, object> dict1)
return dict1.ContainsKey(name);
if (obj is IDictionary<string, JToken> dict2)
return dict2.ContainsKey(name);
return obj.GetType().GetProperty(name) != null;
}
// public static bool PropertyExistsJsonObject(dynamic settings, string name)
// {
// if (settings is ExpandoObject)
// return ((IDictionary<string, object>)settings).ContainsKey(name);
// else if (settings is DynamicJsonObject)
// return ((DynamicJsonObject)settings).GetDynamicMemberNames().Contains(name);
// return settings.GetType().GetProperty(name) != null;
// }
}
doesPropertyExist
Time:59.5907507s Memory:403680
HasProperty
Time:30.8231781s Memory:14968
IsPropertyExist
Time:39.6179575s Memory:97000
IsPropertyExistBinderException throw find
PropertyExists
Time:56.009761s Memory:13464
PropertyExistsJToken
Time:61.6146953s Memory:15952