Javascript 有没有可能在C语言中有一个隐式反序列化参数的方法,如果它是';s作为JSON字符串传递? 问题:

Javascript 有没有可能在C语言中有一个隐式反序列化参数的方法,如果它是';s作为JSON字符串传递? 问题:,javascript,c#,asp.net-mvc,asp.net-core,asp.net-core-viewcomponent,Javascript,C#,Asp.net Mvc,Asp.net Core,Asp.net Core Viewcomponent,我有一些ViewComponents看起来是这样的: public IViewComponentResult Invoke(BuyerViewModel buyer) 我希望他们能够接受一个buyervewmodel,或者一个表示buyervewmodel的JSON字符串。例如,当您从JavaScript向控制器方法传递JSON时,如果该方法需要类型为Dog的参数,则控制器会自动尝试将JSON反序列化为Dog的实例。我试着模仿那种行为 目标是这两个示例都能起作用: var buyer = ne

我有一些ViewComponents看起来是这样的:

public IViewComponentResult Invoke(BuyerViewModel buyer)
我希望他们能够接受一个
buyervewmodel
,或者一个表示
buyervewmodel
的JSON字符串。例如,当您从JavaScript向控制器方法传递JSON时,如果该方法需要类型为
Dog
的参数,则控制器会自动尝试将JSON反序列化为
Dog
的实例。我试着模仿那种行为

目标是这两个示例都能起作用:

var buyer = new BuyerSummaryViewModel() { FirstName = "John" };
ViewComponent("Buyer", buyer);

为什么? 我正在尝试创建一个通用JavaScript方法,可以动态获取ViewComponent:

const fetchViewComponent=async(viewComponentName,viewModel)=>{
让数据={viewComponentName,viewModel};
让html=wait$.get(`/Order/FetchViewComponent`,data);
返回html;
}
//获取BuyerViewComponent(示例)
(异步()=>{
让component=wait-fetchViewComponent(“买方”,`@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Buyer))`);
控制台日志(组件);
})();

我试过的 如果我指定ViewModel是一个
BuyerViewModel
,它就可以工作。JSON字符串自动反序列化为
BuyerViewModel

public class FetchViewComponentRequest
{
  public string ViewComponentName { get; set; }
  public BuyerViewModel ViewModel { get; set; }
  //     ^^^^^^^^^^^^^^
}

[HttpGet]
public IActionResult FetchViewComponent(FetchViewComponentRequest request)
{
  return ViewComponent(request.ViewComponentName, request.ViewModel);
}

问题 但是,我不想指定类型;我希望这是通用的。所以我试了一下:

public class FetchViewComponentRequest
{
  public string ViewComponentName { get; set; }
  public string ViewModel { get; set; }
  //     ^^^^^^
}

[HttpGet]
public IActionResult FetchViewComponent(FetchViewComponentRequest request)
{
  return ViewComponent(request.ViewComponentName, request.ViewModel);
}
但正如所料,
request.ViewModel
不是正确的类型;它在
Invoke
方法中结束
null
。我希望可以指定一个标志或更全局的东西,以便它尝试隐式地将该字符串反序列化为所需的类型

有没有一种我没有考虑过的更简单的方法?或者,如果不是,我所设想的方式是否可能


(我使用的是.NETCore2.2

是否可以将您的FetchViewComponentRequest设置为通用

    public class FetchViewComponentRequest<T>
    {
        public string ViewComponentName { get; set; }
        public T ViewModel { get; set; }
        //     ^^^^^^^^^^^^^^
    }

    [HttpGet]
    public IActionResult FetchViewComponent(FetchViewComponentRequest<BuyerViewModel> request)
    {
        return ViewComponent(request.ViewComponentName, request.ViewModel);
    }
公共类FetchViewComponentRequest
{
公共字符串ViewComponentName{get;set;}
公共T视图模型{get;set;}
//     ^^^^^^^^^^^^^^
}
[HttpGet]
公共IActionResult FetchViewComponent(FetchViewComponentRequest请求)
{
返回ViewComponent(request.ViewComponentName、request.ViewModel);
}

是否可以将您的FetchViewComponentRequest设置为通用

    public class FetchViewComponentRequest<T>
    {
        public string ViewComponentName { get; set; }
        public T ViewModel { get; set; }
        //     ^^^^^^^^^^^^^^
    }

    [HttpGet]
    public IActionResult FetchViewComponent(FetchViewComponentRequest<BuyerViewModel> request)
    {
        return ViewComponent(request.ViewComponentName, request.ViewModel);
    }
公共类FetchViewComponentRequest
{
公共字符串ViewComponentName{get;set;}
公共T视图模型{get;set;}
//     ^^^^^^^^^^^^^^
}
[HttpGet]
公共IActionResult FetchViewComponent(FetchViewComponentRequest请求)
{
返回ViewComponent(request.ViewComponentName、request.ViewModel);
}

该方法需要了解对象的类型

public T Convert<T>(dynamic obj) where T:class,new()
    {
        T myob = null;

        if (obj !=null && obj is T)
        {
            myob = obj as T;

        }
        else if (obj is string)
        {
            //convert to type
            myob = JsonConvert.DeserializeObject<T>(obj);

        }

        return myob;
    }
public T Convert(动态对象),其中T:class,new()
{
T myob=null;
如果(obj!=null&&obj为T)
{
myob=obj为T;
}
else if(obj是字符串)
{
//转换为类型
myob=JsonConvert.DeserializeObject(obj);
}
返回肌瘤;
}

该方法需要了解对象的类型

public T Convert<T>(dynamic obj) where T:class,new()
    {
        T myob = null;

        if (obj !=null && obj is T)
        {
            myob = obj as T;

        }
        else if (obj is string)
        {
            //convert to type
            myob = JsonConvert.DeserializeObject<T>(obj);

        }

        return myob;
    }
public T Convert(动态对象),其中T:class,new()
{
T myob=null;
如果(obj!=null&&obj为T)
{
myob=obj为T;
}
else if(obj是字符串)
{
//转换为类型
myob=JsonConvert.DeserializeObject(obj);
}
返回肌瘤;
}

好的,我不确定你需要什么

但是这里有一个动态的方法来实现它,不需要指定

现在您可以使用简单的反序列化对象

public object ToObject(DynamicType dynamicType){
var type = Type.GetType(dynamicType.TypeName);
// Here you could check if the json is list, its really upp to you
// but as an example, i will still add it
if (dynamicType.JsonString.StartsWith("[")) // its a list
    type =(List<>).MakeGenericType(type);
return JsonConvert.DeserializeObject(dynamicType.JsonString, type);
} 

好的,我不确定你需要什么

但是这里有一个动态的方法来实现它,不需要指定

现在您可以使用简单的反序列化对象

public object ToObject(DynamicType dynamicType){
var type = Type.GetType(dynamicType.TypeName);
// Here you could check if the json is list, its really upp to you
// but as an example, i will still add it
if (dynamicType.JsonString.StartsWith("[")) // its a list
    type =(List<>).MakeGenericType(type);
return JsonConvert.DeserializeObject(dynamicType.JsonString, type);
} 

你只是想要一个泛型方法,它可以接受一个泛型并找出它的类型吗?不一定。我想要一个不需要定义所有可能类型的解决方案。最佳情况下,我会传递一个JSON字符串来代替ViewModel,它会自动将其反序列化为所需的类型。如果它对该类型没有任何了解,它如何知道要创建什么类型?请注意,在我问题的“我尝试了什么”部分,我定义了请求应该是
BuyerViewModel
。但是,我的JavaScript只传递一个JSON字符串。有一个隐式的反序列化-代码实现了“哦,这应该是一个
BuyerViewModel
-我将尝试反序列化它。”我希望这种行为发生在
Invoke
方法中。你只是想要一个泛型方法,它可以接受泛型并确定生成它的类型吗?不一定。我想要一个不需要定义所有可能类型的解决方案。最佳情况下,我会传递一个JSON字符串来代替ViewModel,它会自动将其反序列化为所需的类型。如果它对该类型没有任何了解,它如何知道要创建什么类型?请注意,在我问题的“我尝试了什么”部分,我定义了请求应该是
BuyerViewModel
。但是,我的JavaScript只传递一个JSON字符串。有一个隐式的反序列化-代码实现了“哦,这应该是一个
BuyerViewModel
-我将尝试反序列化它。”我希望在
Invoke
方法中发生这种行为。谢谢!不幸的是,我不相信这真的实现了我的目标。我试图避免在
buyerwiwmodel
视图组件之外的任何地方硬编码类型
buyerwiwmodel
FetchViewComponent
方法应该适用于所有视图组件,而不仅仅是
Buyer
。谢谢!不幸的是,我不相信这真的实现了我的目标。我试图避免硬编码