本文整理汇总了C#中System.Xml.Serialization.TypeMapping.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# TypeMapping.GetType方法的具体用法?C# TypeMapping.GetType怎么用?C# TypeMapping.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.TypeMapping
的用法示例。
在下文中一共展示了TypeMapping.GetType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportDefaultValue
object ImportDefaultValue(TypeMapping mapping, string defaultValue) {
if (defaultValue == null)
return DBNull.Value;
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (!(mapping is PrimitiveMapping)) {
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
}
else if (mapping.IsList) {
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
}
#endif
if (mapping is EnumMapping) {
EnumMapping em = (EnumMapping)mapping;
ConstantMapping[] c = em.Constants;
if (em.IsFlags) {
Hashtable values = new Hashtable();
string[] names = new string[c.Length];
long[] ids = new long[c.Length];
for (int i = 0; i < c.Length; i++) {
ids[i] = em.IsFlags ? 1L << i : (long)i;
names[i] = c[i].Name;
values.Add(c[i].XmlName, ids[i]);
}
// this validates the values
long val = XmlCustomFormatter.ToEnum(defaultValue, values, em.TypeName, true);
return XmlCustomFormatter.FromEnum(val, names, ids);
}
else {
for (int i = 0; i < c.Length; i++) {
if (c[i].XmlName == defaultValue)
return c[i].Name;
}
}
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, defaultValue, em.TypeDesc.FullName));
}
// Primitive mapping
PrimitiveMapping pm = (PrimitiveMapping)mapping;
if (!pm.TypeDesc.HasDefaultSupport) {
#if DEBUG
Debug.WriteLineIf(CompModSwitches.XmlSerialization.TraceVerbose, "XmlSerialization::Dropping default value for " + pm.TypeDesc.Name);
#endif
return DBNull.Value;
}
if (!pm.TypeDesc.HasCustomFormatter) {
if (pm.TypeDesc.FormatterName == "String")
return defaultValue;
Type formatter = typeof(XmlConvert);
MethodInfo format = formatter.GetMethod("To" + pm.TypeDesc.FormatterName, new Type[] {typeof(string)});
if (format != null) {
return format.Invoke(formatter, new Object[] {defaultValue});
}
#if DEBUG
Debug.WriteLineIf(CompModSwitches.XmlSerialization.TraceVerbose, "XmlSerialization::Failed to GetMethod " + formatter.Name + ".To" + pm.TypeDesc.FormatterName);
#endif
}
else {
return XmlCustomFormatter.ToDefaultValue(defaultValue, pm.TypeDesc.FormatterName);
}
return DBNull.Value;
}
示例2: AddDefaultValueAttribute
void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object value, TypeMapping mapping) {
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (!(mapping is PrimitiveMapping)) {
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name));
}
else if (mapping.IsList) {
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Default value is invalid for " + mapping.GetType().Name));
}
#endif
if (value == null) return;
CodeExpression valueExpression = null;
CodeExpression initExpression = null;
CodeExpression typeofValue = null;
string typeName = mapping.TypeDesc.FullName;
Type type = value.GetType();
CodeAttributeArgument[] arguments = null;
if (mapping is EnumMapping) {
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (value.GetType() != typeof(string)) throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Invalid enumeration type " + value.GetType().Name));
#endif
if (((EnumMapping)mapping).IsFlags) {
string[] values = ((string)value).Split(null);
for (int i = 0; i < values.Length; i++) {
if (values[i].Length == 0) continue;
CodeExpression enumRef = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeName), values[i]);
if (valueExpression != null)
valueExpression = new CodeBinaryOperatorExpression(valueExpression, CodeBinaryOperatorType.BitwiseOr, enumRef);
else
valueExpression = enumRef;
}
}
else {
valueExpression = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeName), (string)value);
}
initExpression = valueExpression;
arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(valueExpression)};
}
else if (type == typeof(bool) ||
type == typeof(Int32) ||
type == typeof(string) ||
type == typeof(double)) {
initExpression = valueExpression = new CodePrimitiveExpression(value);
arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(valueExpression)};
}
else if (type == typeof(Int16) ||
type == typeof(Int64) ||
type == typeof(float) ||
type == typeof(byte) ||
type == typeof(decimal)) {
valueExpression = new CodePrimitiveExpression(value.ToString());
typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName));
arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
}
else if (type == typeof(sbyte) ||
type == typeof(UInt16) ||
type == typeof(UInt32) ||
type == typeof(UInt64)) {
// need to promote the non-CLS complient types
value = PromoteType(type, value);
valueExpression = new CodePrimitiveExpression(value.ToString());
typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName));
arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
initExpression = new CodeCastExpression(type.FullName, new CodePrimitiveExpression(value));
}
else if (type == typeof(DateTime)) {
DateTime dt = (DateTime)value;
string dtString;
long ticks;
if (mapping.TypeDesc.FormatterName == "Date") {
dtString = XmlCustomFormatter.FromDate(dt);
ticks = (new DateTime(dt.Year, dt.Month, dt.Day)).Ticks;
}
else if (mapping.TypeDesc.FormatterName == "Time") {
dtString = XmlCustomFormatter.FromDateTime(dt);
ticks = dt.Ticks;
}
else {
dtString = XmlCustomFormatter.FromDateTime(dt);
ticks = dt.Ticks;
}
valueExpression = new CodePrimitiveExpression(dtString);
typeofValue = new CodeTypeOfExpression(CodeIdentifier.EscapeKeywords(type.FullName));
arguments = new CodeAttributeArgument[] {new CodeAttributeArgument(typeofValue), new CodeAttributeArgument(valueExpression)};
initExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(DateTime)), new CodeExpression[] {new CodePrimitiveExpression(ticks)});
}
if (arguments != null) {
if (field != null) field.InitExpression = initExpression;
AddCustomAttribute(metadata, typeof(DefaultValueAttribute), arguments);
}
}