[英]C# Get an object which is a property and execute a method in the object from a string value
我有一个具有属性对象的类。 这些对象中的每一个都有方法。 从字符串值/变量中,我想获取这个属性,它是一个对象并执行它的方法。
var property = this.GetType().GetProperty( "SomePropertyObject" );
MethodInfo getMethod = property.GetMethod;
var obj = getMethod.Invoke......
var runMethod = obj.GetType().GetMethod( "SomeMethod" );
runMethod.Invoke(obj, new object[] { value1 } );
问题是当我获得属性时,我只有 get 和 set 方法可以使用。 如果我可以通过 get 方法获取对象,我就可以运行对象方法。 请你告诉我这是否可行,我该如何去做?
非常感谢您的帮助。
编辑:
基本上我想将审计添加到我的实体框架 DbContext 类中。 我已经成功地为较小的项目创建了一个单表审计,但现在我想使用反射创建每表审计。 我能够创建一个审计模型,该模型与其各自的模型相同,具有 AuditId 和 Action(更新、添加、软删除等)。 我使用反射在 Model 和 ModelAudit 之间传输属性值。
我想更改将 ModelAudit 添加到相应 DbSet 集合的当前 switch 语句。
private void SaveAudit( DbEntityEntry entry )
{
try
{
var name = entry.Entity.GetType().Name;
var space = entry.Entity.GetType().Namespace;
var type = Type.GetType( string.Format( "{0}.Audits.{1}Audit", space, name ) );
object audit = null;
if ( type != null )
{
audit = Activator.CreateInstance( type );
var props =
entry.Entity.GetType()
.GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy );
audit.GetType().GetProperty( "Action" ).SetValue( audit, entry.State.ToString() );
foreach ( var prop in props )
{
if ( prop.CanWrite && prop.CanRead )
switch ( prop.PropertyType.ToString() )
{
case "System.String":
case "System.string":
case "System.Int32":
case "System.Nullable`1[System.Int32]":
case "System.Int64":
case "System.bool":
case "System.Nullable`1[System.Boolean]":
case "System.DateTime":
case "System.Nullable`1[System.DateTime]":
audit.GetType().GetProperty( prop.Name ).SetValue( audit, prop.GetValue( entry.Entity ) );
break;
}
}
// TODO: Replace with reflection.
if ( !entry.State.ToString().Equals( "Unchanged" ) )
{
switch ( name )
{
case "Account":
AccountAudits.Add( ( AccountAudit ) audit );
如果我正确理解您的问题,您可以执行以下操作:
public static object RunMethod(object o, string propertyName, string methodName, object[] methodArguments = null)
{
var properyInfo = o.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Static)
if (properyInfo == null)
//No such property, bail out appropiately
var invokeTarget = properyInfo.GetValue(o);
if (invokeTarget == null)
//Property returned null, bail out appropiately
var argumentTypes = methodArguments == null ? Enumerable.Empty<Type>() : methodArguments.Select(a => a.GetType());
var methodInfo = invokeTarget.GetType().GetMethod(methodName, argumentTypes.ToArray());
if (methodInfo == null)
//No such method, bail out appropiately
return methodInfo.Invoke(invokeTarget, methodArguments);
}
这是通过反射完成的,可以通过以下方式完成:
Type type = Assembly.GetType(some_type);
MethodInfo method = type.GetMethod(name_of_method);
ParameterInfo[] parameters = method?.GetParameters();
object instance = Activator.CreateInstance(type, null);
var result = method.Invoke(instance, parameters);
这未经测试,但我相信这是您寻求答案的一般方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.