通用编辑器模板

时间:2016-10-11 19:40:14

标签: c# generics razor asp.net-mvc-5 system.reflection

是否可以使用System.Reflection创建通用视图?  

因此,loop class properties创建inputs @Html.EditorFor@Html.Editor甚至是普通<input /> template。  

有了这个,我可以创建一个 sub replace { my ($search, $replacement, $subject) = @_; if (ref($search) eq "HASH") { print "r is a reference to a HASH.\n"; } elsif (ref($search) eq "SCALAR") { print "r is a reference to a SCALAR.\n"; } elsif (ref($search) eq "ARRAY") { print "r is a reference to a ARRAY.\n"; } } my $str = "Foo"; my @arr = ("Foo"); replace($str); replace(@arr); 并重新使用它来节省时间并为用户创建一个愉快的图像。

有可能吗?

1 个答案:

答案 0 :(得分:0)

是的,有可能,视图的模型应该是对象,控制器可以是通用控制器,但是您应该构建自己的ControllerControllerFactory,如:

    public class MyGenericControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext reqContext, string controllerName)
        {
            if(is controlleName generic) //you should define how build controller name when is generic
            //it should contain the controller name and the generic type name
            {
                string contRealName = "";//get the real controller name from controllerName var
                string genTypeName = ""; //get the generic type name fomr controllerName var
                Type contType = Type.GetType(contRealName);
                Type genType = Type.GetType(genTypeName);
                contType = contType.MakeGenericType(genType);
                return an instance of contType from your IoC container
            }
            else
            {
                Type contType = Type.GetType(controllerName);
                return an instance of contType from your IoC container
            }
        }
    }