无法在运行时 C# MVC 的模型中设置“类型”参数

本文关键字:设置 类型 参数 模型 运行时 MVC | 更新日期: 2023-09-27 18:35:01

我正在尝试在运行时设置方法的类型。当用户从下拉列表中选择类型时,会发生这种情况。有许多不同的类型都继承自同一接口。

这是我解释问题的代码...

接口:

public interface IFoo
{
     string Id { get; set; }
     string Title { get; set; }
}

实现IFoo的类:

Foo1Foo2Foo3Foo4 , ...

类名称放入List<string>中,以在我的 MVC 视图的下拉列表中显示它们:

private static IEnumerable<string> GetAllFooItems()
        {
            var a = typeof(IFoo).Assembly;
            var itemTypes = from type in a.GetTypes()
                            where type.GetInterfaces().Contains(typeof(IFoo))
                            select Activator.CreateInstance(type) as IFoo;
            return itemTypes.Select(instance => instance.GetType().Name).ToList();
        }

将上述List作为下拉列表IEnumerable<SelectListItem>的方法:

private static IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<string> elements)
        {
            return elements.Select(element => new SelectListItem
            {
                Value = element,
                Text = element
            }).ToList();
        }

用户从IFoo项的下拉列表中选择后,我想使用我的模型在以下 get 方法上设置IFoo类型:

public T Get<T>(string id) where T : IFoo
        {
            // do something
        }

这是我的模型

public class FooModel
    {
        [DisplayName("Item Id: ")]
        public string Id { get; set; }
        [DisplayName("Content Type: ")]
        public IFoo FooItem { get; set; }
        public IEnumerable<SelectListItem> FooItems { get; set; }
    }

还有我的控制器

public class FooController : Controller
    {
        FooClient client = new FooClient("Foo"); // Placement of my Get Method (above)
        [HttpGet]
        public ActionResult FooSearch()
        {
            var fooTypes = GetAllFooItems();
            var model = new FooModel();
            model.FooItems = GetSelectListItems(fooTypes);
            return View(model);
        }
        [HttpPost]
        public ActionResult FooSearch(FooModel model)
        {
            var fooTypes = GetAllFooItems();
            model.FooItems = GetSelectListItems(fooTypes);
            client.Get<model.FooItem>(model.id); // model.FooItem does not work
 // !!! I cannot set the Type from the model...
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            return View();
        }
   }

如果有人对如何根据下拉选择更改方法中的 Type 参数有想法,我将非常乐意找到答案。如果您需要更多信息,请告诉我。

无法在运行时 C# MVC 的模型中设置“类型”参数

可以使用

反射来调用Get方法,如下所示:

var result = (IFoo) client.GetType()
    //get the generic Get<T> method
    .GetMethod("Get", new Type[] {typeof (string)}) 
    //get the specific Get<model.FooItem> method
    .MakeGenericMethod(model.FooItem.GetType()) 
    .Invoke(client, new object[] { model.id }); //Invoke the method

顺便说一下,在获取可能类型名称的代码中,不必创建每种类型的实例来获取名称。相反,您可以执行以下操作:

return (from type in a.GetTypes()
        where type.GetInterfaces().Contains(typeof(IFoo))
        select type.Name).ToList();