Код: Выделить всё
public class ResponseBuilder
{
private static ModuleBuilder _ModuleBuilder;
private static Dictionary _ResponseTypes = new Dictionary();
private static object _lock = new object();
///
/// Create a new type 'Response_T' based on Data.Response with Object set to List
///
public static Type CreateResponseType()
{
lock (_lock)
{
if (_ModuleBuilder == null)
{
AssemblyName aname = new AssemblyName("ResponseBuilder");
AppDomain currentDomain = AppDomain.CurrentDomain;
AssemblyBuilder abuilder = currentDomain.DefineDynamicAssembly(aname,
AssemblyBuilderAccess.Run);
_ModuleBuilder = abuilder.DefineDynamicModule("ResponseClasses");
}
// Set new type name to 'Response_T'
string newTypeName = "Response_" + typeof(T).FullName;
// Check if this type has already been created and stored in dictionary
if (_ResponseTypes.ContainsKey(newTypeName))
{
return _ResponseTypes[newTypeName];
}
// Create the new type
TypeBuilder typeBuilder = _ModuleBuilder.DefineType(newTypeName,
TypeAttributes.Public
| TypeAttributes.Class
| TypeAttributes.AutoClass
| TypeAttributes.AnsiClass
| TypeAttributes.ExplicitLayout);
int fieldOffset = 0;
FieldBuilder fieldBuilder = typeBuilder.DefineField("Status", typeof(Data.Response.ResponseStatus), FieldAttributes.Public);
fieldBuilder.SetOffset(fieldOffset);
fieldOffset += IntPtr.Size;
fieldBuilder = typeBuilder.DefineField("ErrorMessage", typeof(string), FieldAttributes.Public);
fieldBuilder.SetOffset(fieldOffset);
fieldOffset += IntPtr.Size;
fieldBuilder = typeBuilder.DefineField("ErrorDetails", typeof(string), FieldAttributes.Public);
fieldBuilder.SetOffset(fieldOffset);
fieldOffset += IntPtr.Size;
fieldBuilder = typeBuilder.DefineField("Logon", typeof(Data.Logon), FieldAttributes.Public);
fieldBuilder.SetOffset(fieldOffset);
fieldOffset += IntPtr.Size;
fieldBuilder = typeBuilder.DefineField("Object", typeof(List), FieldAttributes.Public);
fieldBuilder.SetOffset(fieldOffset);
fieldOffset += IntPtr.Size;
// Add newly create type to static list
Type type = typeBuilder.CreateType();
_ResponseTypes.Add(newTypeName, type);
return type;
}
}
}
Код: Выделить всё
Response response = new Response();
Type dynamicResponseType = ResponseBuilder.CreateResponseType();
JsonSerializer serializer = new JsonSerializer();
System.IO.Stream stream = httpresponse.Content.ReadAsStreamAsync().Result;
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
{
using (JsonTextReader jtr = new JsonTextReader(sr))
{
var x = serializer.Deserialize(jtr, dynamicResponseType);
FieldInfo fi = dynamicResponseType.GetField("Status");
response.Status = (Response.ResponseStatus)fi.GetValue(x);
fi = dynamicResponseType.GetField("ErrorMessage");
response.ErrorMessage = (string)fi.GetValue(x);
fi = dynamicResponseType.GetField("ErrorDetails");
response.ErrorDetails = (string)fi.GetValue(x);
fi = dynamicResponseType.GetField("Logon");
response.Logon = (Logon)fi.GetValue(x);
fi = dynamicResponseType.GetField("Object");
response.Object = fi.GetValue(x);
}
}
Теперь, к моей проблеме. Я перехожу от Newtonsoft.json на System.text.json в .NET Framework 4.8. Я использую System.text.json Версия 9.0.6.
Код: Выделить всё
JsonTextReaderЯ пытался:
Код: Выделить всё
var x = JsonSerializer.Deserialize(httpresponse.Content.ReadAsStreamAsync().Result, dynamicResponseType);
< /code>
xПодробнее здесь: https://stackoverflow.com/questions/796 ... n-system-t
Мобильная версия