我遇到了与上周相同的问题。不幸的是,目前Azure函数(甚至是2.x)不支持持久功能的多态性。持久上下文将您的对象序列化为JSON,但是无法传递GitHub中所述的JSON序列化设置。也有another issue关于这个特定问题。
就我而言,我有一个抽象基类,但是您可以对派生类型使用相同的方法。您可以创建一个自定义JSON转换器,该转换器将处理反序列化期间选择正确的类型。因此,例如,如果您具有这种继承:
[JsonConverter(typeof(DerivedTypeConverter))]
public abstract class Base
{
[JsonProperty("$type")]
public abstract string Type { get; }
}
public class Child : Base
{
public override string Type => nameof(Child);
}
public class Child2 : Base
{
public override string Type => nameof(Child2);
}
然后,您可以拥有一个JSON转换器:
public class BaseDerivedTypeConverter : DefaultContractResolver
{
// You need this to protect yourself against circular dependencies
protected override JsonConverter ResolveContractConverter(Type objectType)
{
return typeof(Base).IsAssignableFrom(objectType) && !objectType.IsAbstract
? null
: base.ResolveContractConverter(objectType);
}
}
public class DerivedTypeConverter : JsonConverter
{
private static readonly JsonSerializerSettings Settings =
new JsonSerializerSettings()
{
ContractResolver = new BaseDerivedTypeConverter()
};
public override bool CanConvert(Type objectType) => (objectType == typeof(Base));
public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer)
{
JObject jsonObject = JObject.Load(reader);
// Make checks if jsonObject["$type"].Value<string>() has a supported type
// You can have a static dictionary or a const array of supported types
// You can leverage the array or dictionary to get the type you want again
var type = Type.GetType("Full namespace to the type you want",false); // the false flag means that the method call won't throw an exception on error
if (type != null)
{
return JsonConvert.DeserializeObject(jsonObject.ToString(),type,Settings);
}
else
{
throw new ValidationException("No valid $type has been specified!");
}
}
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) => throw new NotImplementedException();
}
在我使用context.GetInput<Base>()
时,我可以得到Child
或Child1
,因为Base
是抽象的。
根据您的实际情况,它可以是Book
或Student
。这也适用于其他持久功能操作,例如
var foobar = await context.CallActivityAsync<Base>("FuncName",context.GetInput<int>());
转换器将处理此问题,您将在foobar
中获得所需的对象。
,
根据我的理解,“教科书”类是“书”的扩展,因此“书”是父类,“教科书”是子类。在您的上下文中,您想将子类(教科书)转换为父类(书本)。 此后,“ book”将仅具有属性“ title”(这是它们的常用属性),而没有特定的属性“ classfor” 。您可以参考以下代码:
,
跟踪更新,以将Json序列化传递给Azure Functions here,显示该更新将在v2.1中发布!
本文链接:https://www.f2er.com/3143463.html