简体   繁体   English

将Json对象作为类型为System.Object或Interface的对象传递

[英]Pass Json object as object of type System.Object or Interface not working

Class: 类:

public class ClassNameA :ISomeInterface { } public class ClassNameB :ISomeInterface { } 公共类ClassNameA:ISomeInterface {}公共类ClassNameB:ISomeInterface {}

From javascript: 从javascript:

var reqP = { 'Id': id, 'Name':name };
var ReqParams = { 'ReqParams': reqP };
var obj = { 'ClassNameA': ReqParams };

makeAjaxCall("POST",
        JSON.stringify(obj), '/ControllerName/someMethod/', 'html',

Action method looks like: 动作方法如下:

public ActionResult someMethod(object obj){
    // call comes to this method but obj is not populated. 
}

public ActionResult someMethod(ISomeInterface obj){
    // call comes to this method but throws exception. 
    // Exception : Cannot instantiate interface. but i am passing class object.
}

from JavaScript I will pass object of a concrete class type which implements ISomeInterface so that I can have multiple implementations. 从JavaScript中,我将传递实现ISomeInterface的具体类类型的对象,以便可以进行多种实现。 Concrete Class can of any one of the two types. 这两种类型中的任何一种的混凝土类罐头。

Any Suggestions? 有什么建议么?

That won't work. 那行不通。 The model binder needs a concrete type to be able to create an instance and bind the values. 模型绑定器需要一个具体的类型才能创建实例并绑定值。

object is a concrete type and can be created using Activator.CreateInstance , but it doesn't have any properties that will match the data you receive. object是一种具体类型,可以使用Activator.CreateInstance创建,但是它没有任何与接收到的数据匹配的属性。

The interface (or abstract types) cannot be created, since they're not concrete types. 由于接口(或抽象类型)不是具体类型,因此无法创建。 It's a simple as that. 就这么简单。

If the JSON contained some hint about the type, it migth be possible to implement a custom model binder to create the instances for you. 如果JSON包含有关类型的一些提示,则可以实现自定义模型绑定程序来为您创建实例。 You can read more about the model binding process here . 您可以在此处阅读有关模型绑定过程的更多信息。

The default model binder is not going to be able to figure out what to do. 默认模型联编程序将无法弄清楚该怎么做。 Let's just go through the exercise with some sample interfaces to show why. 让我们通过一些示例界面来完成本练习,以说明原因。 Say you have this: 说你有这个:

public ActionResult SomeMethod(ISomeInterface obj)
{
    // ...
}

...and say you have these two implemenetations: ...并说您有以下两种实现:

class ClassA : ISomeInterface
{
    public string Name { get; set; }
    public string Phone { get; set; }
}

class ClassB : ISomeInterface
{
    public string Name { get; set; }
    public string Email { get; set; }
}

...and say this JSON is posted: ...并说发布了此JSON:

{ "Name": "Some Value" }

How would the model binder know which class to use? 模型绑定器将如何知道要使用哪个类? Would it search all defined classes in all assemblies to find all implementations of the interface? 是否会在所有程序集中搜索所有已定义的类以找到该接口的所有实现? And if so, even if it had smart selection logic based on properties, how would it select between ClassA or ClassB , both of which are compatible? 如果是这样,即使它具有基于属性的智能选择逻辑,也将如何在ClassAClassB之间进行选择?

What you may want to do is either use a type like Dictionary<string, object> that you know will be compatible, dynamic , or go with a concrete class that is a union of everything you need. 您可能想要做的是使用像Dictionary<string, object>这样的类型,您知道该类型将是兼容, dynamic ,或者使用一个具体类,该类将您所需的一切结合在一起。 Alternately, you can create a custom model binder with its own logic for selecting the class you want to instantiate. 或者,您可以使用其自己的逻辑来创建自定义模型绑定程序,以选择要实例化的类。 See this question for more details. 有关更多详细信息,请参见此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM