110,545
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取实例的属性值
/// </summary>
/// <param name="ClassInstance">类实例</param>
/// <param name="PropertyName">属性名</param>
/// <returns>属性值</returns>
private object GetPropertyValue(object ClassInstance, string PropertyName)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
return myPropertyInfo.GetValue(ClassInstance, null);
}
/// <summary>
/// 设置实例的属性值
/// </summary>
/// <param name="ClassInstance">类实例</param>
/// <param name="PropertyName">属性名</param>
private void SetPropertyValue(object ClassInstance, string PropertyName, object PropertyValue)
{
Type myType = ClassInstance.GetType();
PropertyInfo myPropertyInfo = myType.GetProperty(PropertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
myPropertyInfo.SetValue(ClassInstance, PropertyValue, null);
}
/// <summary>
/// 执行实例的方法
/// </summary>
/// <param name="ClassInstance">类实例</param>
/// <param name="MethodName">方法名</param>
/// <param name="parameters">参数列表</param>
/// <returns>返回值</returns>
private object MethodInvoke(object ClassInstance, string MethodName, object[] parameters)
{
if (parameters == null)
{
parameters = new object[0];
}
Type myType = ClassInstance.GetType();
Type[] types = new Type[parameters.Length];
for (int i = 0; i < parameters.Length; ++i)
{
types[i] = parameters[i].GetType();
}
MethodInfo myMethodInfo = myType.GetMethod(MethodName, types);
return myMethodInfo.Invoke(ClassInstance, parameters);
}
//這里用TextBox做實驗,當然只要有Text屬性的并且有get存取子就可以使用
TextBox textbox = new TextBox();
object value = text;
string text = value.GetType().GetProperty("Text").GetValue(value, null);
public void MyFunciton(object a)
{
object value = Activator.CreateInstance(a.GetType());
((Control)value).Text = "";
}
public void MyFunciton(object a)
{
object value = Activator.CreateInstance(a.GetType());
}