GetOriginalTypeParameterType throws对象引用未设置为对象异常的实例 [英] GetOriginalTypeParameterType throws Object reference not set to an instance of an object exception

查看:127
本文介绍了GetOriginalTypeParameterType throws对象引用未设置为对象异常的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考:动态如何用作通用?

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>())
 {
 }
}

$ C> CheckEntity(16, 容器); 。第一行运行后,当使用调试器检查时, AnyObject 将成为一个空白的 Assembly.Models.DbModels.Container 。如果 var AnyType = AnyObject.GetType(); ,则 AnyType 显示为程序集.Models.DbModels.Container 。但是,当调用 CheckWithInference(AnyObject,entityId); 被抛出异常时。

This enters with CheckEntity(16,"Container");. After the first line runs, AnyObject becomes a blank Assembly.Models.DbModels.Container when inspected with the debugger. If var AnyType = AnyObject.GetType(); is used, then AnyType shows as Assembly.Models.DbModels.Container. However, when the call to CheckWithInference(AnyObject, entityId); is made an exception is thrown.

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

  • outer: Object reference not set to an instance of an object.
  • inner: Microsoft.CSharp.RuntimeBinder.SymbolTable.GetOriginalTypeParameterType(Type t) +10

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

    I made a self-contained example here - but it runs without error :(

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

    Note, this is in asp.net mvc 3 c#

    HomeController.cs:

    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; }
    }
    }
    

    Index.cshtml

    Index.cshtml

    @{
    ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    @Html.ActionLink("Start", "CheckEntity", new { entityId = 16, entityType = "Example" })
    

    我很失落。为什么会得到这个例外?我无法轻易地复制它。我不知道还有什么要包括的例子,因为这是所有的实际的代码所在。

    I am at a loss. Why am getting this exception? I was unable to easily reproduce it. I am not sure what else to include for the example as this is all that the actual code has in it.

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

    The really confusing part is that in the application, when this exception occurs, the action fails. However, upon revisiting the page and trying a second time, there is no exception thrown.

    推荐答案

    正如C#聊天室中所讨论的,这里的解决方案是完全绕过动态,并使用反射来调用通用方法。动态具有一些不错的功能,但有时会导致比它更有价值的麻烦,特别是在运行时可以获取Type对象。

    As discussed in C# chat room, the solution here is to bypass dynamic entirely and use reflection to invoke the generic method. Dynamic has some nice features but sometimes causes more trouble than it's worth, especially when it's possible to get the Type object at runtime.

    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 });
    

    很高兴帮助:)

    这篇关于GetOriginalTypeParameterType throws对象引用未设置为对象异常的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆