本文整理汇总了C#中ClassDataContract类的典型用法代码示例。如果您正苦于以下问题:C# ClassDataContract类的具体用法?C# ClassDataContract怎么用?C# ClassDataContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ClassDataContract类属于命名空间,在下文中一共展示了ClassDataContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DemandSerializationFormatterPermission
private void DemandSerializationFormatterPermission(ClassDataContract classContract)
{
if (!classContract.HasDataContract && !classContract.IsNonAttributedType)
{
this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.DemandSerializationFormatterPermissionMethod);
}
}
示例2: ConvertDictionaryToClassDataContract
public static object ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary<string, object> deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
{
if (deserialzedValue == null)
{
return null;
}
if (dataContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
var tuple = deserialzedValue["DateTime"] as Tuple<DateTime, string>;
DateTimeOffset dto = new DateTimeOffset(tuple != null ? tuple.Item1 : (DateTime)deserialzedValue["DateTime"]);
return dto.ToOffset(new TimeSpan(0, (int)deserialzedValue["OffsetMinutes"], 0));
}
object serverTypeStringValue;
if (deserialzedValue.TryGetValue(JsonGlobals.ServerTypeString, out serverTypeStringValue))
{
dataContract = ResolveDataContractFromTypeInformation(serverTypeStringValue.ToString(), dataContract, context);
}
object o = CreateInstance(dataContract);
CheckDuplicateNames(dataContract);
DataContractJsonSerializer.InvokeOnDeserializing(o, dataContract, context);
ReadClassDataContractMembers(serializer, dataContract, deserialzedValue, o, context);
DataContractJsonSerializer.InvokeOnDeserialized(o, dataContract, context);
if (dataContract.IsKeyValuePairAdapter)
{
return dataContract.GetKeyValuePairMethodInfo.Invoke(o, Array.Empty<Type>());
}
return o;
}
示例3: CreateObject
private void CreateObject(ClassDataContract classContract)
{
Type typeOfValueType = this.objectType = classContract.UnderlyingType;
if (typeOfValueType.IsValueType && !classContract.IsNonAttributedType)
{
typeOfValueType = Globals.TypeOfValueType;
}
this.objectLocal = this.ilg.DeclareLocal(typeOfValueType, "objectDeserialized");
if (classContract.UnderlyingType == Globals.TypeOfDBNull)
{
this.ilg.LoadMember(Globals.TypeOfDBNull.GetField("Value"));
this.ilg.Stloc(this.objectLocal);
}
else if (classContract.IsNonAttributedType)
{
if (typeOfValueType.IsValueType)
{
this.ilg.Ldloca(this.objectLocal);
this.ilg.InitObj(typeOfValueType);
}
else
{
this.ilg.New(classContract.GetNonAttributedTypeConstructor());
this.ilg.Stloc(this.objectLocal);
}
}
else
{
this.ilg.Call(null, XmlFormatGeneratorStatics.GetUninitializedObjectMethod, DataContract.GetIdForInitialization(classContract));
this.ilg.ConvertValue(Globals.TypeOfObject, typeOfValueType);
this.ilg.Stloc(this.objectLocal);
}
}
示例4: AddClassDataContract
private void AddClassDataContract(ClassDataContract classDataContract)
{
if (classDataContract.BaseContract != null)
{
this.Add(classDataContract.BaseContract.StableName, classDataContract.BaseContract);
}
if (!classDataContract.IsISerializable && (classDataContract.Members != null))
{
for (int i = 0; i < classDataContract.Members.Count; i++)
{
DataMember dataMember = classDataContract.Members[i];
DataContract memberTypeDataContract = this.GetMemberTypeDataContract(dataMember);
if ((this.dataContractSurrogate != null) && (dataMember.MemberInfo != null))
{
object obj2 = DataContractSurrogateCaller.GetCustomDataToExport(this.dataContractSurrogate, dataMember.MemberInfo, memberTypeDataContract.UnderlyingType);
if (obj2 != null)
{
this.SurrogateDataTable.Add(dataMember, obj2);
}
}
this.Add(memberTypeDataContract.StableName, memberTypeDataContract);
}
}
this.AddKnownDataContracts(classDataContract.KnownDataContracts);
}
示例5: GenerateClassWriter
internal XmlFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
{
if (_useReflection)
{
return new ReflectionXmlFormatWriter().ReflectionWriteClass;
}
else
{
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForWrite(null);
try
{
_ilg.BeginMethod("Write" + classContract.StableName.Name + "ToXml", Globals.TypeOfXmlFormatClassWriterDelegate, memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForWrite(securityException);
}
else
{
throw;
}
}
InitArgs(classContract.UnderlyingType);
WriteClass(classContract);
return (XmlFormatClassWriterDelegate)_ilg.EndMethod();
}
}
示例6: GenerateClassReader
public JsonFormatClassReaderDelegate GenerateClassReader(ClassDataContract classContract)
{
_ilg = new CodeGenerator();
bool memberAccessFlag = classContract.RequiresMemberAccessForRead(null);
try
{
BeginMethod(_ilg, "Read" + DataContract.SanitizeTypeName(classContract.StableName.Name) + "FromJson", typeof(JsonFormatClassReaderDelegate), memberAccessFlag);
}
catch (SecurityException securityException)
{
if (memberAccessFlag)
{
classContract.RequiresMemberAccessForRead(securityException);
}
else
{
throw;
}
}
InitArgs();
CreateObject(classContract);
_ilg.Call(_contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, _objectLocal);
InvokeOnDeserializing(classContract);
if (classContract.IsISerializable)
ReadISerializable(classContract);
else
ReadClass(classContract);
if (Globals.TypeOfIDeserializationCallback.IsAssignableFrom(classContract.UnderlyingType))
{
_ilg.Call(_objectLocal, JsonFormatGeneratorStatics.OnDeserializationMethod, null);
}
InvokeOnDeserialized(classContract);
if (!InvokeFactoryMethod(classContract))
{
_ilg.Load(_objectLocal);
// Do a conversion back from DateTimeOffsetAdapter to DateTimeOffset after deserialization.
// DateTimeOffsetAdapter is used here for deserialization purposes to bypass the ISerializable implementation
// on DateTimeOffset; which does not work in partial trust.
if (classContract.UnderlyingType == Globals.TypeOfDateTimeOffsetAdapter)
{
_ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfDateTimeOffsetAdapter);
_ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetMethod);
_ilg.ConvertValue(Globals.TypeOfDateTimeOffset, _ilg.CurrentMethod.ReturnType);
}
//Copy the KeyValuePairAdapter<K,T> to a KeyValuePair<K,T>.
else if (classContract.IsKeyValuePairAdapter)
{
_ilg.Call(classContract.GetKeyValuePairMethodInfo);
_ilg.ConvertValue(Globals.TypeOfKeyValuePair.MakeGenericType(classContract.KeyValuePairGenericArguments), _ilg.CurrentMethod.ReturnType);
}
else
{
_ilg.ConvertValue(_objectLocal.LocalType, _ilg.CurrentMethod.ReturnType);
}
}
return (JsonFormatClassReaderDelegate)_ilg.EndMethod();
}
示例7: ReflectionWriteMembers
internal int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, ClassDataContract derivedMostClassContract)
{
int memberCount = (classContract.BaseContract == null) ? 0 :
ReflectionWriteMembers(xmlWriter, obj, context, classContract.BaseContract, derivedMostClassContract);
Type classType = classContract.UnadaptedClassType;
XmlDictionaryString[] memberNames = classContract.MemberNames;
XmlDictionaryString ns = classContract.Namespace;
context.IncrementItemCount(classContract.Members.Count);
for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
{
DataMember member = classContract.Members[i];
Type memberType = member.MemberType;
if (member.IsGetOnlyCollection)
{
context.StoreIsGetOnlyCollection();
}
bool writeXsiType = CheckIfMemberHasConflict(member, classContract, derivedMostClassContract);
MemberInfo memberInfo = member.MemberInfo;
object memberValue = ReflectionGetMemberValue(obj, memberInfo);
if (writeXsiType || !ReflectionTryWritePrimitive(xmlWriter, context, memberType, memberValue, member.MemberInfo, null /*arrayItemIndex*/, ns, memberNames[i] /*nameLocal*/, i + _childElementIndex))
{
ReflectionWriteStartElement(memberType, ns, ns.Value, member.Name, 0);
if (classContract.ChildElementNamespaces[i + _childElementIndex] != null)
{
var nsChildElement = classContract.ChildElementNamespaces[i + _childElementIndex];
_arg0XmlWriter.WriteNamespaceDecl(nsChildElement);
}
ReflectionWriteValue(memberType, memberValue, writeXsiType);
ReflectionWriteEndElement();
}
}
return 0;
}
示例8: ReflectionWriteClass
public void ReflectionWriteClass(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, XmlDictionaryString[] memberNames)
{
InvokeOnSerializing(obj, context, classContract);
obj = ResolveAdapterType(obj, classContract);
ReflectionWriteMembers(xmlWriter, obj, context, classContract, classContract, 0 /*childElementIndex*/, memberNames);
InvokeOnSerialized(obj, context, classContract);
}
示例9: ProcessClassDataContract
static DataContract ProcessClassDataContract(ClassDataContract contract, ExportContext context, MemberInfo memberNode)
{
string prefix = context.SetNamespace(contract.Namespace.Value);
foreach (DataMember member in GetDataMembers(contract))
{
if (member.MemberInfo.Name == memberNode.Name && member.MemberInfo.DeclaringType.IsAssignableFrom(memberNode.DeclaringType))
{
context.WriteChildToContext(member, prefix);
return member.MemberTypeContract;
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.QueryGeneratorPathToMemberNotFound)));
}
示例10: CheckDuplicateNames
private static void CheckDuplicateNames(ClassDataContract dataContract)
{
if (dataContract.MemberNames != null)
{
Dictionary<string, object> memberTable = new Dictionary<string, object>();
for (int i = 0; i < dataContract.MemberNames.Length; i++)
{
if (memberTable.ContainsKey(dataContract.MemberNames[i].Value))
{
throw new SerializationException(SR.Format(SR.JsonDuplicateMemberInInput, dataContract.MemberNames[i].Value));
}
memberTable.Add(dataContract.MemberNames[i].Value, null);
}
}
}
示例11: ReadClassDataContractMembers
private static void ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary<string, object> deserialzedValue, object newInstance, XmlObjectSerializerReadContextComplexJson context)
{
if (dataContract.BaseContract != null)
{
ReadClassDataContractMembers(serializer, dataContract.BaseContract, deserialzedValue, newInstance, context);
}
for (int i = 0; i < dataContract.Members.Count; i++)
{
DataMember member = dataContract.Members[i];
object currentMemberValue;
if (deserialzedValue.TryGetValue(XmlConvert.DecodeName(dataContract.Members[i].Name), out currentMemberValue) ||
dataContract.IsKeyValuePairAdapter && deserialzedValue.TryGetValue(XmlConvert.DecodeName(dataContract.Members[i].Name.ToLowerInvariant()), out currentMemberValue))
{
if (member.MemberType.GetTypeInfo().IsPrimitive || currentMemberValue == null)
{
SetMemberValue(newInstance, serializer.ConvertObjectToDataContract(member.MemberTypeContract, currentMemberValue, context), dataContract.Members[i].MemberInfo, dataContract.UnderlyingType);
}
else
{
context.PushKnownTypes(dataContract);
object subMemberValue = serializer.ConvertObjectToDataContract(member.MemberTypeContract, currentMemberValue, context);
Type declaredType = (member.MemberType.GetTypeInfo().IsGenericType && member.MemberType.GetGenericTypeDefinition() == Globals.TypeOfNullable)
? Nullable.GetUnderlyingType(member.MemberType)
: member.MemberType;
if (!(declaredType == Globals.TypeOfObject && subMemberValue.GetType() == Globals.TypeOfObjectArray) && declaredType != subMemberValue.GetType())
{
DataContract memberValueContract = DataContract.GetDataContract(subMemberValue.GetType());
context.CheckIfTypeNeedsVerifcation(member.MemberTypeContract, memberValueContract);
}
if (member.IsGetOnlyCollection)
{
PopulateReadOnlyCollection(newInstance, member, (IEnumerable)subMemberValue);
}
else
{
SetMemberValue(newInstance, subMemberValue, dataContract.Members[i].MemberInfo, dataContract.UnderlyingType);
}
context.PopKnownTypes(dataContract);
}
}
else if (member.IsRequired)
{
XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(dataContract.MemberNames[i].Value, dataContract.UnderlyingType);
}
}
}
示例12: ReflectionInitArgs
private void ReflectionInitArgs(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract)
{
if (obj.GetType() == typeof(DateTimeOffset))
{
obj = DateTimeOffsetAdapter.GetDateTimeOffsetAdapter((DateTimeOffset)obj);
}
else if (obj.GetType().GetTypeInfo().IsGenericType && obj.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
obj = classContract.KeyValuePairAdapterConstructorInfo.Invoke(new object[] { obj });
}
_arg0XmlWriter = xmlWriter;
_arg1Object = obj;
_arg2Context = context;
_arg3ClassDataContract = classContract;
}
示例13: ProcessClassDataContract
private static DataContract ProcessClassDataContract(ClassDataContract contract, ExportContext context, MemberInfo memberNode)
{
string prefix = context.SetNamespace(contract.Namespace.Value);
if (contract.Members != null)
{
foreach (DataMember member in contract.Members)
{
if (member.MemberInfo == memberNode)
{
context.WriteChildToContext(member, prefix);
return member.MemberTypeContract;
}
}
}
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("QueryGeneratorPathToMemberNotFound")));
}
示例14: GetDataMembers
static IEnumerable<DataMember> GetDataMembers(ClassDataContract contract)
{
if (contract.BaseContract != null)
{
foreach (DataMember baseClassMember in GetDataMembers(contract.BaseContract))
{
yield return baseClassMember;
}
}
if (contract.Members != null)
{
foreach (DataMember member in contract.Members)
{
yield return member;
}
}
}
示例15: GenerateClassWriter
internal JsonFormatClassWriterDelegate GenerateClassWriter(ClassDataContract classContract)
{
try
{
if (TD.DCJsonGenWriterStartIsEnabled())
{
TD.DCJsonGenWriterStart("Class", classContract.UnderlyingType.FullName);
}
return helper.GenerateClassWriter(classContract);
}
finally
{
if (TD.DCJsonGenWriterStopIsEnabled())
{
TD.DCJsonGenWriterStop();
}
}
}