GetMethod 返回空 c#

斯库特

我不明白为什么GetMethod回来null- 两次在method1method

Type model = AssemblyHelper.GetTypeByClassName(Assembly.GetExecutingAssembly(), modelName + MappingColums.Tokens.Validation); // MappingColums.Tokens.Validation = "Validation"

出于测试目的,我尝试了两次 get 方法:

MethodInfo method1 = model.GetType().GetMethod(MappingColums.Tokens.Get + modelName); // MappingColums.Tokens.Get + modelName = "Get" + "Product"
MethodInfo method = model.GetType().GetMethods().FirstOrDefault(x => x.Name == MappingColums.Tokens.Get + modelName && x.GetParameters().Count() == 0);
object result = method.Invoke(model, null);

下面是我想通过反射使用的类和方法。

public class ProductValidation
{
    private IProductRepository repositoryProduct;

    public ProductValidation(IProductRepository repoProduct)
    {
        repositoryProduct = repoProduct;
    }

    public ICollection<Product> GetProduct()
    {
        return repositoryProduct.Products.ToList();
    }

}

我不知道这是否重要,但我正在使用ASP.NET Core MVC. Type model在后面调试的AssemblyHelper.GetTypeByClassName地方是Declared Methods: {System.Collections.Generic.ICollection1[AureliaCMS.Models.Entities.Product] GetProduct()}

请给我建议在哪里查找问题。

编辑:非常感谢 - 你们都是对的 -Klaus Gütter而且Aldert

工作代码

    public static class AssemblyHelper
    {
        public static IEnumerable<Type> GetLoadableTypes(Assembly assembly)
        {
            if (assembly == null) throw new ArgumentNullException(nameof(assembly));
            try
            {
                return assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException e)
            {
                return e.Types.Where(t => t != null);
            }
        }
        public static Type GetTypeByClassName(Assembly assembly, string className)
        {
            if (assembly == null) throw new ArgumentNullException(nameof(assembly));
            return AssemblyHelper.GetLoadableTypes(assembly).Where(a => a.Name == className).FirstOrDefault();
        }
}

Type model = AssemblyHelper.GetTypeByClassName(Assembly.GetExecutingAssembly(), MappingColums.Tokens.EF + modelName + MappingColums.Tokens.Repository);
MethodInfo method = model.GetMethods().FirstOrDefault(x => x.Name == MappingColums.Tokens.Get + modelName + MappingColums.Tokens.Validation && x.GetParameters().Count() == 0);
ConstructorInfo constructor = model.GetConstructor(new[] { typeof(AureliaCMSStoreContext) });
object instanceConstructor = constructor.Invoke(new object[] { context });
object instanceMethod = method.Invoke(instanceConstructor, null);
警长

你的代码永远不会工作。您需要一个对象实例来执行该方法。就像是:

    Type model = AssemblyHelper.GetTypeByClassName(Assembly.GetExecutingAssembly(), modelName + MappingColums.Tokens.Validation);

    MethodInfo method = model.GetMethod(MappingColums.Tokens.Get + modelName);

    myClass myObject = new myObject();

    object result = method.Invoke(myObject , null);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

GetMethod返回null

来自分类Dev

C#反射-GetMethod

来自分类Dev

Type.GetMethod返回始终为null

来自分类Dev

为什么反射GetMethod返回null?

来自分类Dev

C#JsonConvertDeserialization返回空值

来自分类Dev

ZipArchive返回空文件C#

来自分类Dev

用C读取文件返回空

来自分类Dev

在C函数调用中返回空项目

来自分类Dev

C#XmlDocument选择节点返回空

来自分类Dev

c# 中的 FLWOR XQuery 返回空?

来自分类Dev

C++。如何返回空指针而不是函数返回类型?

来自分类Dev

为什么getmethod即使该方法存在也返回null

来自分类Dev

在System .__ ComObject上调用GetMethod()始终返回null

来自分类Dev

ZipArchive返回空文件夹C#

来自分类Dev

在C#中解析JSON返回空值

来自分类Dev

WSDL Web服务从C#返回空数组

来自分类Dev

C#string [] arrayA在arrayA [3]中返回空值

来自分类Dev

C#将XML解析为列表返回空列表

来自分类Dev

对C#类的肥皂响应始终返回空值

来自分类Dev

c ++:std :: function实例的目标返回空指针

来自分类Dev

如何从F#返回空元组到C#?

来自分类Dev

反序列化返回空对象C#

来自分类Dev

c_str()返回空字符串

来自分类Dev

获取后代节点总是返回空 XML C#

来自分类Dev

C#:拆分返回空字符串

来自分类Dev

Bing Visual Search C# SDK 返回空响应

来自分类Dev

C#属性可以没有GetMethod和SetMethod吗?

来自分类Dev

C#类型GetMethod(),其中参数顺序是随机的

来自分类Dev

返回空?