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,"Container");, После запуска первой строкиAnyObject становится пустымAssembly.Models.DbModels.Container при проверке с помощью отладчика. Еслиvar AnyType = AnyObject.GetType(); используется, тоAnyType показывает какAssembly.Models.DbModels.Container, Тем не менее, когда звонокCheckWithInference(AnyObject, entityId); сделано исключение брошено.

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 :(

Note, this is in 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>();
    }

}
}

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

@{
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.

Ответы на вопрос(1)

Ваш ответ на вопрос