CPF/CPF.ReoGrid/Utility/CachedTypeWrapper.cs
2024-06-24 10:15:59 +08:00

65 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
namespace CPF.ReoGrid.Utility
{
internal class CachedTypeWrapper
{
public Type Type { get; private set; }
public CachedTypeWrapper(Type type)
{
this.Type = type;
}
public object StaticInvoke(string name, params object[] args)
{
return this.Invoke(null, name, args);
}
public object Invoke(object instance, string name, params object[] args)
{
MethodInfo methodInfo = null;
bool flag = !this.cachedMethods.TryGetValue(name, out methodInfo);
if (flag)
{
methodInfo = this.Type.GetMethod(name, ((instance == null) ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.NonPublic);
}
return (methodInfo == null) ? null : methodInfo.Invoke(instance, args);
}
public object GetProperty(object instance, string name)
{
PropertyInfo propertyInfo = null;
bool flag = !this.cachedProperties.TryGetValue(name, out propertyInfo);
if (flag)
{
propertyInfo = this.Type.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic);
this.cachedProperties[name] = propertyInfo;
}
return (propertyInfo == null) ? null : propertyInfo.GetValue(instance, null);
}
public void SetProperty(object instance, string name, object value)
{
PropertyInfo propertyInfo = null;
bool flag = !this.cachedProperties.TryGetValue(name, out propertyInfo);
if (flag)
{
propertyInfo = this.Type.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic);
this.cachedProperties[name] = propertyInfo;
}
bool flag2 = propertyInfo != null;
if (flag2)
{
propertyInfo.SetValue(instance, name, null);
}
}
private Dictionary<string, MethodInfo> cachedMethods = new Dictionary<string, MethodInfo>();
private Dictionary<string, PropertyInfo> cachedProperties = new Dictionary<string, PropertyInfo>();
}
}