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

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

问题描述

参考:如何动态地作为一种通用的

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,容器); 。第一线运行后, 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.

  • 外:对象引用不设置到对象的实例。
  • 内:Microsoft.CSharp.RuntimeBinder.SymbolTable.GetOriginalTypeParameterType(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>();
        }
    
    }
    }
    

    Example.cs

    Example.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" })
    

    我很茫然。为什么我得到这个例外?我无法很容易地复制它。我不知道还有什么要包括的例子,因为这是所有实际code中有。

    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抛出不设置到对象异常的实例对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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