本文整理匯總了C#中System.ComponentModel.TypeConverter.ConvertToString方法的典型用法代碼示例。如果您正苦於以下問題:C# TypeConverter.ConvertToString方法的具體用法?C# TypeConverter.ConvertToString怎麽用?C# TypeConverter.ConvertToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.ComponentModel.TypeConverter
的用法示例。
在下文中一共展示了TypeConverter.ConvertToString方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CDSAttributesRetrieve
/// <summary>
/// This method retrieves a set of CDSAttribute from a class.
/// </summary>
/// <param name="data">The class to examine.</param>
/// <param name="conv">A converter function to turn the value in to a string.</param>
/// <returns>Returns an enumerable collection of attributes and values.</returns>
public static IEnumerable<KeyValuePair<CDSAttributeAttribute, string>> CDSAttributesRetrieve(
this IXimuraContent data, TypeConverter conv)
{
List<KeyValuePair<PropertyInfo, CDSAttributeAttribute>> attrList =
AH.GetPropertyAttributes<CDSAttributeAttribute>(data.GetType());
foreach (KeyValuePair<PropertyInfo, CDSAttributeAttribute> reference in attrList)
{
PropertyInfo pi = reference.Key;
if (pi.PropertyType == typeof(string))
{
yield return new KeyValuePair<CDSAttributeAttribute, string>(
reference.Value, pi.GetValue(data, null) as string);
}
else if (pi.PropertyType == typeof(IEnumerable<string>))
{
IEnumerable<string> enumerator = pi.GetValue(data, null) as IEnumerable<string>;
foreach (string value in enumerator)
yield return new KeyValuePair<CDSAttributeAttribute, string>(
reference.Value, value);
}
else if (conv != null && conv.CanConvertFrom(pi.PropertyType))
{
yield return new KeyValuePair<CDSAttributeAttribute, string>(
reference.Value, conv.ConvertToString(pi.GetValue(data, null)));
}
}
}
示例2: test1
public void test1()
{
Rectangle rect = new Rectangle(10, 12, 14, 16);
// Wrong
System.ComponentModel.TypeConverter baseConverter = new System.ComponentModel.TypeConverter();
string sample1 = baseConverter.ConvertToString(rect);
// Right
System.ComponentModel.TypeConverter rectSpecificConverter = TypeDescriptor.GetConverter(rect);
string sample2 = rectSpecificConverter.ConvertToString(rect);
log.DebugFormat("From new TypeConverter() {0}\r\n", sample1);
log.DebugFormat("From TypeDescriptor.GetConverter() {0}\r\n", sample2);
log.DebugFormat("From rect.ToString() {0}\r\n", rect.ToString());
}
示例3: CreateToolItem
private ListViewItem CreateToolItem(NamedShellLink tool, TypeConverter keysConverter)
{
ListViewItem item = new ListViewItem(tool.Name) {
Tag = tool
};
ListViewItem.ListViewSubItem item2 = item.SubItems.Add(keysConverter.ConvertToString(tool.Hotkey));
if (tool.Hotkey == Keys.None)
{
item.UseItemStyleForSubItems = false;
item2.ForeColor = SystemColors.GrayText;
}
return item;
}
示例4: ConvertObjectToTypeInternal
private static bool ConvertObjectToTypeInternal(object o, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject)
{
IDictionary<string, object> dictionary = o as IDictionary<string, object>;
if (dictionary != null)
{
return ConvertDictionaryToObject(dictionary, type, serializer, throwOnError, out convertedObject);
}
IList list = o as IList;
if (list != null)
{
IList list2;
if (ConvertListToObject(list, type, serializer, throwOnError, out list2))
{
convertedObject = list2;
return true;
}
convertedObject = null;
return false;
}
if ((type == null) || (o.GetType() == type))
{
convertedObject = o;
return true;
}
//TypeDescriptor.GetConverter(type) !!!
TypeConverter converter = new TypeConverter();
if (converter.CanConvertFrom(o.GetType()))
{
try
{
convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, o);
return true;
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return false;
}
}
if (converter.CanConvertFrom(typeof(string)))
{
try
{
string str;
if (o is DateTime)
{
DateTime time = (DateTime)o;
str = time.ToUniversalTime().ToString("u", CultureInfo.InvariantCulture);
}
else
{
//ConvertToInvariantString(o); !!!
str = converter.ConvertToString(o);
}
//ConvertFromInvariantString(str); !!!
convertedObject = converter.ConvertToString(str);
return true;
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return false;
}
}
if (type.IsAssignableFrom(o.GetType()))
{
convertedObject = o;
return true;
}
if (throwOnError)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, AtlasWeb.JSON_CannotConvertObjectToType, new object[] { o.GetType(), type }));
}
convertedObject = null;
return false;
}
示例5: UpdateCommandShortcut
private void UpdateCommandShortcut(ListViewItem item, Keys shortcutKeys, TypeConverter keysConverter)
{
ListViewItem.ListViewSubItem item2 = (item.SubItems.Count > 1) ? item.SubItems[1] : item.SubItems.Add("-");
if (keysConverter == null)
{
keysConverter = TypeDescriptor.GetConverter(typeof(Keys));
}
item2.Text = keysConverter.ConvertToString(shortcutKeys);
item2.ForeColor = SystemColors.GrayText;
item.UseItemStyleForSubItems = shortcutKeys != Keys.None;
}
示例6: CDSReferencesRetrieve
/// <summary>
/// This method retrieves a set of reference attributes from a class.
/// </summary>
/// <param name="data">The class to examine.</param>
/// <param name="conv">A converter function to turn the value in to a string.</param>
/// <returns>Returns an enumerable collection of attributes and values.</returns>
public static IEnumerable<KeyValuePair<CDSReferenceAttribute, string>> CDSReferencesRetrieve(
this IXimuraContent data, TypeConverter conv)
{
List<KeyValuePair<PropertyInfo, CDSReferenceAttribute>> attrList =
AH.GetPropertyAttributes<CDSReferenceAttribute>(data.GetType());
foreach (KeyValuePair<PropertyInfo, CDSReferenceAttribute> reference in attrList)
{
PropertyInfo pi = reference.Key;
if (pi.PropertyType != typeof(string) &&
(conv == null || !conv.CanConvertFrom(pi.PropertyType)))
continue;
string value;
if (pi.PropertyType == typeof(string))
value = pi.GetValue(data, null) as string;
else
value = conv.ConvertToString(pi.GetValue(data, null));
yield return new KeyValuePair<CDSReferenceAttribute, string>(reference.Value, value);
}
}