本文整理汇总了C#中IValueSerializerContext.GetValueSerializerFor方法的典型用法代码示例。如果您正苦于以下问题:C# IValueSerializerContext.GetValueSerializerFor方法的具体用法?C# IValueSerializerContext.GetValueSerializerFor怎么用?C# IValueSerializerContext.GetValueSerializerFor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IValueSerializerContext
的用法示例。
在下文中一共展示了IValueSerializerContext.GetValueSerializerFor方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSerializerFor
public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
{
ValueSerializer s = context.GetValueSerializerFor (type);
if (s == null) {
DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
s = defaultContext.GetValueSerializerFor (type);
}
return s;
}
示例2: GetSerializerFor
// untested
public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor, IValueSerializerContext context)
{
if (descriptor == null)
throw new ArgumentNullException ("descriptor");
if (context != null)
return context.GetValueSerializerFor (descriptor);
var tc = descriptor.Converter;
if (tc != null && tc.GetType () != typeof (TypeConverter))
return new TypeConverterValueSerializer (tc);
return null;
}
示例3: GetSerializerFor
// untested
public static ValueSerializer GetSerializerFor (PropertyInfo descriptor, IValueSerializerContext context)
{
if (descriptor == null)
throw new ArgumentNullException ("descriptor");
if (context != null)
return context.GetValueSerializerFor (descriptor);
var typeConverterInfo = descriptor.GetCustomAttribute<TypeConverterAttribute> ();
var typeConverterName = typeConverterInfo?.ConverterTypeName;
if (string.IsNullOrEmpty (typeConverterName))
return null;
var tcType = Type.GetType(typeConverterName);
var tc = Activator.CreateInstance (tcType) as TypeConverter;
if (tc != null && tc.GetType () != typeof (TypeConverter))
return new TypeConverterValueSerializer (tc);
return null;
}
示例4: CanConvertToString
public override bool CanConvertToString(object value, IValueSerializerContext context)
{
if (context == null || context.GetValueSerializerFor(typeof(Type)) == null)
return false;
// Can only convert routed commands
RoutedCommand command = value as RoutedCommand;
if (command == null || command.OwnerType == null)
{
return false;
}
if (CommandConverter.IsKnownType(command.OwnerType))
{
return true;
}
else
{
string localName = command.Name + "Command";
Type ownerType = command.OwnerType;
string typeName = ownerType.Name;
// Get them from Properties
PropertyInfo propertyInfo = ownerType.GetProperty(localName, BindingFlags.Public | BindingFlags.Static);
if (propertyInfo != null)
return true;
// Get them from Fields (ScrollViewer.PageDownCommand is a static readonly field
FieldInfo fieldInfo = ownerType.GetField(localName, BindingFlags.Static | BindingFlags.Public);
if (fieldInfo != null)
return true;
}
return false;
}
示例5: ConvertToString
public override string ConvertToString(object value, IValueSerializerContext context)
{
if (value != null)
{
RoutedCommand command = value as RoutedCommand;
if (null != command && null != command.OwnerType)
{
// Known Commands, so write shorter version
if (CommandConverter.IsKnownType(command.OwnerType))
{
return command.Name;
}
else
{
ValueSerializer typeSerializer = null;
if (context == null)
{
throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name ));
}
// Get the ValueSerializer for the System.Type type
typeSerializer = context.GetValueSerializerFor(typeof(Type));
if (typeSerializer == null)
{
throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name ));
}
return typeSerializer.ConvertToString(command.OwnerType, context) + "." + command.Name + "Command";
}
}
}
else
return string.Empty;
throw GetConvertToException(value, typeof(string));
}
示例6: ConvertFromString
public override object ConvertFromString(string value, IValueSerializerContext context)
{
if (value != null)
{
if (value != String.Empty)
{
Type declaringType = null;
String commandName;
// Check for "ns:Class.Command" syntax.
int dotIndex = value.IndexOf('.');
if (dotIndex >= 0)
{
// We have "ns:Class.Command" syntax.
// Find the type name in the form of "ns:Class".
string typeName = value.Substring(0, dotIndex);
if (context == null)
{
throw new InvalidOperationException(SR.Get(SRID.ValueSerializerContextUnavailable, this.GetType().Name ));
}
// Get the ValueSerializer for the System.Type type
ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
if (typeSerializer == null)
{
throw new InvalidOperationException(SR.Get(SRID.TypeValueSerializerUnavailable, this.GetType().Name ));
}
// Use the TypeValueSerializer to parse the "ns:Class" into a System.Type.
declaringType = typeSerializer.ConvertFromString(typeName, context) as Type;
// Strip out the "Command" part of "ns:Class.Command".
commandName = value.Substring(dotIndex + 1).Trim();
}
else
{
// Assume the known commands
commandName = value.Trim();
}
// Find the command given the declaring type & name (this is shared with CommandConverter)
ICommand command = CommandConverter.ConvertFromHelper( declaringType, commandName );
if (command != null)
{
return command;
}
}
else
{
return null; // String.Empty <==> null , (for roundtrip cases where Command property values are null)
}
}
return base.ConvertFromString(value, context);
}
示例7: GetSerializerFor
public static ValueSerializer GetSerializerFor(Type type, IValueSerializerContext context)
{
if (context != null)
{
ValueSerializer valueSerializerFor = context.GetValueSerializerFor(type);
if (valueSerializerFor != null)
{
return valueSerializerFor;
}
}
return GetSerializerFor(type);
}
示例8: CheckForMarkupExtension
//
// Check for values that can be converted into markup extensions, either because
// they are a well known type ((null, arrays, enums, and types), or because they
// can be type converted into an ME.
//
internal static object CheckForMarkupExtension(
Type propertyType,
object value,
IValueSerializerContext context,
bool convertEnums)
{
// null => NullExtension
if (value == null)
{
return new NullExtension();
}
// See if the type has a type converter that can create a MarkupExtension
// (Have to do this after the null check so that GetConverter doesn't get
// an invalid argument.)
TypeConverter converter = TypeDescriptor.GetConverter(value);
if (converter.CanConvertTo(context, typeof(MarkupExtension)))
{
// The type provides a converter that creates a MarkupExtension.
return converter.ConvertTo(context, TypeConverterHelper.InvariantEnglishUS, value, typeof(MarkupExtension));
}
// System.Type => TypeExtension
Type type = value as Type;
if (type != null)
{
// If the property is declared to be a type already, we don't need to convert it
// into {x:Type} syntax.
if( propertyType == typeof(Type) )
{
return value;
}
return new TypeExtension(type);
}
// Enums => StaticExtension
if (convertEnums)
{
Enum enumValue = value as Enum;
if (enumValue != null)
{
ValueSerializer typeSerializer = context.GetValueSerializerFor(typeof(Type));
Debug.Assert(typeSerializer != null, "typeSerializer for Enum was null");
string typeName = typeSerializer.ConvertToString(enumValue.GetType(), context);
return new StaticExtension(typeName + "." + enumValue.ToString());
}
}
// Arrays => ArrayExtension
Array array = value as Array;
if (array != null)
{
return new ArrayExtension(array);
}
// Otherwise, value is unchanged.
return value;
}