gpt4 book ai didi

c# - GetOriginalTypeParameterType 抛出 Object reference not set to an instance of object 异常

转载 作者:太空狗 更新时间:2023-10-29 20:34:32 26 4
gpt4 key购买 nike

引用:How can a dynamic be used as a generic?

public void CheckEntity(int entityId, string entityType = null)
{
dynamic AnyObject = Activator.CreateInstance("Assembly","Assembly.Models.DbModels." + entityType).Unwrap();
CheckWithInference(AnyObject, entityId);
}

private static void CheckWithInference<T>(T ignored, int entityId) where T : class
{
Check<T>(entityId);
}

private static void Check<T>(int entityId) where T : class
{
using (var gr = new GenericRepository<T>())
{
}
}

这通过 CheckEntity(16,"Container"); 输入。第一行运行后,AnyObject 在使用调试器检查时变为空白 Assembly.Models.DbModels.Container。如果使用 var AnyType = AnyObject.GetType();,则 AnyType 显示为 Assembly.Models.DbModels.Container。但是,当调用 CheckWithInference(AnyObject, entityId); 时会抛出异常。

  • 外部:对象引用未设置为对象的实例。
  • 内部:Microsoft.CSharp.RuntimeBinder.SymbolTable.GetOriginalTypeParameterType(Type t) +10

    我在这里做了一个独立的例子 - 但它运行没有错误:(

    请注意,这是在 asp.net mvc 3 c# 中

    HomeController.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace InferenceExample.Controllers
    {
    public class HomeController : Controller
    {
    //
    // GET: /Home/

    public ActionResult Index()
    {
    return View();
    }

    public void CheckEntity(int entityId, string entityType = null)
    {
    dynamic AnyObject = Activator.CreateInstance("InferenceExample", "InferenceExample.Models." + entityType).Unwrap();
    CheckWithInference(AnyObject, entityId);
    }

    private static void CheckWithInference<T>(T ignored, int entityId) where T : class
    {
    Check<T>(entityId);
    }

    private static void Check<T>(int entityId) where T : class
    {
    var repo = new List<T>();
    }

    }
    }

    示例.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace InferenceExample.Models
    {
    public class Example
    {
    public int ExampleId { get; set; }
    public string Name { get; set; }
    }
    }

    索引.cshtml

    @{
    ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    @Html.ActionLink("Start", "CheckEntity", new { entityId = 16, entityType = "Example" })

    我很茫然。为什么会出现此异常?我无法轻易重现它。我不确定该示例还包括什么,因为这是实际代码中的全部内容。

    真正令人困惑的部分是,在应用程序中,当发生此异常时,操作失败。然而,在重新访问页面并再次尝试时,没有抛出异常。

  • 最佳答案

    正如在 C# 聊天室中讨论的那样,这里的解决方案是完全绕过动态并使用反射来调用泛型方法。 Dynamic 有一些不错的特性,但有时会带来比其值(value)更多的麻烦,尤其是当可以在运行时获取 Type 对象时。

    var modelType = Type.GetType("InferenceExample.Models." + entityType + ",InferenceExample");

    var checkMethod = typeof(HomeController).GetMethod("CheckWithInference", BindingFlags.NonPublic | BindingFlags.Static);
    checkMethod.MakeGenericMethod(modelType).Invoke(null, new object[] { entityId });

    很高兴为您提供帮助:)

    关于c# - GetOriginalTypeParameterType 抛出 Object reference not set to an instance of object 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12096695/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com