本文整理汇总了C#中System.ComponentModel.EnumConverter.ConvertTo方法的典型用法代码示例。如果您正苦于以下问题:C# EnumConverter.ConvertTo方法的具体用法?C# EnumConverter.ConvertTo怎么用?C# EnumConverter.ConvertTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.EnumConverter
的用法示例。
在下文中一共展示了EnumConverter.ConvertTo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertTo_String
public void ConvertTo_String ()
{
EnumConverter converter = new EnumConverter (typeof (E));
Assert.AreEqual ("Bb", converter.ConvertTo (null,
CultureInfo.InvariantCulture, E.Bb,
typeof (string)), "#A1");
Assert.AreEqual ("Dd", converter.ConvertTo (null,
CultureInfo.InvariantCulture, 3,
typeof (string)), "#A2");
Assert.AreEqual (string.Empty, converter.ConvertTo (
null, CultureInfo.InvariantCulture, null,
typeof (string)), "#A3");
Assert.AreEqual ("Cc", converter.ConvertTo (
null, CultureInfo.InvariantCulture, (E) 2,
typeof (string)), "#A4");
Assert.AreEqual ("Cc", converter.ConvertTo (null,
CultureInfo.InvariantCulture, E2.Bb,
typeof (string)), "#A5");
Assert.AreEqual ("Dd", converter.ConvertTo (null,
CultureInfo.InvariantCulture, E.Bb | E.Dd,
typeof (string)), "#A6");
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
(E) 666, typeof (string));
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// The value '666' is not a valid value for the enum 'E'
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsTrue (ex.Message.IndexOf ("'666'") != -1, "#B5");
Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#B6");
Assert.IsNull (ex.ParamName, "#B7");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"Cc", typeof (string));
Assert.Fail ("#C1");
} catch (FormatException ex) {
// Input string was not in a correct format
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
}
converter = new EnumConverter (typeof (E2));
Assert.AreEqual ("Bb", converter.ConvertTo (null,
CultureInfo.InvariantCulture, E2.Bb,
typeof (string)), "#B1");
Assert.AreEqual ("Aa, Bb", converter.ConvertTo (null,
CultureInfo.InvariantCulture, 3,
typeof (string)), "#B2");
Assert.AreEqual (string.Empty, converter.ConvertTo (
null, CultureInfo.InvariantCulture, null,
typeof (string)), "#B3");
Assert.AreEqual ("Bb", converter.ConvertTo (
null, CultureInfo.InvariantCulture, (E2) 2,
typeof (string)), "#B4");
Assert.AreEqual ("Aa, Bb", converter.ConvertTo (
null, CultureInfo.InvariantCulture, E.Dd,
typeof (string)), "#B5");
Assert.AreEqual ("Bb, Dd", converter.ConvertTo (null,
CultureInfo.InvariantCulture, E2.Bb | E2.Dd,
typeof (string)), "#B6");
}
示例2: ConvertTo_InstanceDescriptor_Flags
public void ConvertTo_InstanceDescriptor_Flags ()
{
InstanceDescriptor idesc;
FieldInfo fi;
MethodInfo mi;
ParameterInfo [] parameters;
object [] arguments;
EnumConverter converter = new EnumConverter (typeof (E2));
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E2.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#A1");
Assert.IsNotNull (idesc.Arguments, "#A2");
Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
Assert.IsTrue (idesc.IsComplete, "#A5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#A6");
Assert.AreEqual (typeof (E2), fi.DeclaringType, "#A7");
Assert.AreEqual (typeof (E2), fi.FieldType, "#A8");
Assert.IsTrue (fi.IsStatic, "#A9");
Assert.AreEqual ("Bb", fi.Name, "#A10");
Assert.AreEqual (E2.Bb, fi.GetValue (null), "#A11");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#B1");
Assert.IsNotNull (idesc.Arguments, "#B2");
Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
Assert.IsTrue (idesc.IsComplete, "#B5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#B6");
Assert.AreEqual (typeof (E2), fi.DeclaringType, "#B7");
Assert.AreEqual (typeof (E2), fi.FieldType, "#B8");
Assert.IsTrue (fi.IsStatic, "#B9");
Assert.AreEqual ("Aa", fi.Name, "#B10");
Assert.AreEqual (E2.Aa, fi.GetValue (null), "#B11");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E2.Bb | E2.Dd, typeof (InstanceDescriptor))
as InstanceDescriptor;
Assert.IsNotNull (idesc, "#C1");
Assert.IsNotNull (idesc.Arguments, "#C2");
Assert.AreEqual (2, idesc.Arguments.Count, "#C3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
arguments = (object []) idesc.Arguments;
Assert.AreEqual (typeof (E2), arguments [0], "#C5");
Assert.AreEqual (typeof (int), arguments [1].GetType (), "#C6");
Assert.AreEqual (10, arguments [1], "#C7");
Assert.IsTrue (idesc.IsComplete, "#C8");
mi = idesc.MemberInfo as MethodInfo;
Assert.IsNotNull (mi, "#C9");
Assert.AreEqual ("ToObject", mi.Name, "#C10");
Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#C11");
parameters = mi.GetParameters ();
Assert.AreEqual (2, parameters.Length, "#C12");
Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#C13");
Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#C14");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
"5", typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#D1");
Assert.IsNotNull (idesc.Arguments, "#D2");
Assert.AreEqual (2, idesc.Arguments.Count, "#D3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
arguments = (object []) idesc.Arguments;
Assert.AreEqual (typeof (E2), arguments [0], "#D5");
Assert.AreEqual (typeof (int), arguments [1].GetType (), "#D6");
Assert.AreEqual (5, arguments [1], "#D7");
Assert.IsTrue (idesc.IsComplete, "#D8");
mi = idesc.MemberInfo as MethodInfo;
Assert.IsNotNull (mi, "#D9");
Assert.AreEqual ("ToObject", mi.Name, "#D10");
Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#D11");
parameters = mi.GetParameters ();
Assert.AreEqual (2, parameters.Length, "#D12");
Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#D13");
Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#D14");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
3, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#E1");
Assert.IsNotNull (idesc.Arguments, "#E2");
Assert.AreEqual (2, idesc.Arguments.Count, "#E3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#E4");
arguments = (object []) idesc.Arguments;
Assert.AreEqual (typeof (E2), arguments [0], "#E5");
Assert.AreEqual (typeof (int), arguments [1].GetType (), "#E6");
Assert.AreEqual (3, arguments [1], "#E7");
Assert.IsTrue (idesc.IsComplete, "#E8");
mi = idesc.MemberInfo as MethodInfo;
Assert.IsNotNull (mi, "#E9");
Assert.AreEqual ("ToObject", mi.Name, "#E10");
Assert.AreEqual (typeof (Enum), mi.DeclaringType, "#E11");
parameters = mi.GetParameters ();
Assert.AreEqual (2, parameters.Length, "#E12");
Assert.AreEqual (typeof (Type), parameters [0].ParameterType, "#E13");
Assert.AreEqual (typeof (int), parameters [1].ParameterType, "#E14");
//.........这里部分代码省略.........
示例3: ConvertTo_EnumArray_Flags
public void ConvertTo_EnumArray_Flags ()
{
Enum [] enums;
EnumConverter converter = new EnumConverter (typeof (E2));
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E.Bb, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#A1");
Assert.AreEqual (1, enums.Length, "#A2");
Assert.AreEqual (typeof (E2), enums [0].GetType (), "#A3");
Assert.AreEqual (E2.Aa, enums [0], "#A4");
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E2.Bb, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#B1");
Assert.AreEqual (1, enums.Length, "#B2");
Assert.AreEqual (typeof (E2), enums [0].GetType (), "#B3");
Assert.AreEqual (E2.Bb, enums [0], "#B4");
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
(E2) 0, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#C1");
Assert.AreEqual (0, enums.Length, "#C2");
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
(E2) 18, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#D1");
Assert.AreEqual (2, enums.Length, "#D2");
Assert.AreEqual (typeof (E2), enums [0].GetType (), "#D3");
Assert.AreEqual (E2.Bb, enums [0], "#D4");
Assert.AreEqual (typeof (E2), enums [1].GetType (), "#D5");
Assert.AreEqual ((E2) 16, enums [1], "#D6");
try {
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
5, typeof (Enum [])) as Enum [];
Assert.Fail ("#E1");
} catch (InvalidCastException ex) {
// Unable to cast object of type 'System.Int32'
// to type 'System.Enum'
Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#E2");
Assert.IsNull (ex.InnerException, "#E3");
Assert.IsNotNull (ex.Message, "#E4");
}
try {
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
"2", typeof (Enum [])) as Enum [];
Assert.Fail ("#F1");
} catch (InvalidCastException ex) {
// Unable to cast object of type 'System.String'
// to type 'System.Enum'
Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#F2");
Assert.IsNull (ex.InnerException, "#F3");
Assert.IsNotNull (ex.Message, "#F4");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
null, typeof (Enum []));
Assert.Fail ("#G1");
} catch (NotSupportedException ex) {
// 'EnumConverter' is unable to convert '(null)'
// to 'System.Enum[]'
Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#G2");
Assert.IsNull (ex.InnerException, "#G3");
Assert.IsNotNull (ex.Message, "#G4");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#G5");
Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#G6");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#G7");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"Bb,Cc", typeof (Enum []));
Assert.Fail ("#H1");
} catch (InvalidCastException ex) {
// Unable to cast object of type 'System.String'
// to type 'System.Enum'
Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#H2");
Assert.IsNull (ex.InnerException, "#H3");
Assert.IsNotNull (ex.Message, "#H4");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"2,4", typeof (Enum []));
Assert.Fail ("#I1");
} catch (InvalidCastException ex) {
// Unable to cast object of type 'System.String'
// to type 'System.Enum'
Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#I2");
Assert.IsNull (ex.InnerException, "#I3");
Assert.IsNotNull (ex.Message, "#I4");
}
converter = new EnumConverter (typeof (F2));
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
(F2) 15, typeof (Enum [])) as Enum [];
//.........这里部分代码省略.........
示例4: ConvertTo_InstanceDescriptor
public void ConvertTo_InstanceDescriptor ()
{
InstanceDescriptor idesc;
FieldInfo fi;
EnumConverter converter = new EnumConverter (typeof (E));
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E.Bb, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#A1");
Assert.IsNotNull (idesc.Arguments, "#A2");
Assert.AreEqual (0, idesc.Arguments.Count, "#A3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#A4");
Assert.IsTrue (idesc.IsComplete, "#A5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#A6");
Assert.AreEqual (typeof (E), fi.DeclaringType, "#A7");
Assert.AreEqual (typeof (E), fi.FieldType, "#A8");
Assert.IsTrue (fi.IsStatic, "#A9");
Assert.AreEqual ("Bb", fi.Name, "#A10");
Assert.AreEqual (E.Bb, fi.GetValue (null), "#A11");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
"2", typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#B1");
Assert.IsNotNull (idesc.Arguments, "#B2");
Assert.AreEqual (0, idesc.Arguments.Count, "#B3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#B4");
Assert.IsTrue (idesc.IsComplete, "#B5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#B6");
Assert.AreEqual (typeof (E), fi.DeclaringType, "#B7");
Assert.AreEqual (typeof (E), fi.FieldType, "#B8");
Assert.IsTrue (fi.IsStatic, "#B9");
Assert.AreEqual ("Cc", fi.Name, "#B10");
Assert.AreEqual (E.Cc, fi.GetValue (null), "#B11");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
2, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#C1");
Assert.IsNotNull (idesc.Arguments, "#C2");
Assert.AreEqual (0, idesc.Arguments.Count, "#C3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#C4");
Assert.IsTrue (idesc.IsComplete, "#C5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#C6");
Assert.AreEqual (typeof (E), fi.DeclaringType, "#C7");
Assert.AreEqual (typeof (E), fi.FieldType, "#C8");
Assert.IsTrue (fi.IsStatic, "#C9");
Assert.AreEqual ("Cc", fi.Name, "#C10");
Assert.AreEqual (E.Cc, fi.GetValue (null), "#C11");
idesc = converter.ConvertTo (null, CultureInfo.InvariantCulture,
(E) 2, typeof (InstanceDescriptor)) as InstanceDescriptor;
Assert.IsNotNull (idesc, "#D1");
Assert.IsNotNull (idesc.Arguments, "#D2");
Assert.AreEqual (0, idesc.Arguments.Count, "#D3");
Assert.AreEqual (typeof (object []), idesc.Arguments.GetType (), "#D4");
Assert.IsTrue (idesc.IsComplete, "#D5");
fi = idesc.MemberInfo as FieldInfo;
Assert.IsNotNull (fi, "#D6");
Assert.AreEqual (typeof (E), fi.DeclaringType, "#D7");
Assert.AreEqual (typeof (E), fi.FieldType, "#D8");
Assert.IsTrue (fi.IsStatic, "#D9");
Assert.AreEqual ("Cc", fi.Name, "#D10");
Assert.AreEqual (E.Cc, fi.GetValue (null), "#D11");
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
null, typeof (InstanceDescriptor));
Assert.Fail ("#E1");
} catch (NotSupportedException ex) {
// 'EnumConverter' is unable to convert '(null)'
// to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'
Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#E2");
Assert.IsNull (ex.InnerException, "#E3");
Assert.IsNotNull (ex.Message, "#E4");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#E5");
Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#E6");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (InstanceDescriptor).FullName + "'") != -1, "#E7");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"5", typeof (InstanceDescriptor));
Assert.Fail ("#F1");
} catch (ArgumentException ex) {
// The value '5' is not a valid value for the enum 'E'
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
Assert.IsNull (ex.InnerException, "#F3");
Assert.IsNotNull (ex.Message, "#F4");
Assert.IsTrue (ex.Message.IndexOf ("'5'") != -1, "#F5");
Assert.IsTrue (ex.Message.IndexOf ("'E'") != -1, "#F6");
Assert.IsNull (ex.ParamName, "#F7");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"Cc", typeof (InstanceDescriptor));
//.........这里部分代码省略.........
示例5: ConvertTo_EnumArray
public void ConvertTo_EnumArray ()
{
Enum [] enums;
EnumConverter converter = new EnumConverter (typeof (E));
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
E.Bb, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#A1");
Assert.AreEqual (1, enums.Length, "#A2");
Assert.AreEqual (typeof (E), enums [0].GetType (), "#A3");
Assert.AreEqual (E.Bb, enums [0], "#A4");
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
2, typeof (Enum [])) as Enum [];
Assert.IsNotNull (enums, "#B1");
Assert.AreEqual (1, enums.Length, "#B2");
Assert.AreEqual (typeof (E), enums [0].GetType (), "#B3");
Assert.AreEqual (E.Cc, enums [0], "#B4");
try {
enums = converter.ConvertTo (null, CultureInfo.InvariantCulture,
"2", typeof (Enum [])) as Enum [];
Assert.Fail ("#C1");
} catch (ArgumentException ex) {
// The value passed in must be an enum base or an
// underlying type for an enum, such as an Int32
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
Assert.IsNotNull (ex.ParamName, "#C5");
Assert.AreEqual ("value", ex.ParamName, "#C6");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
null, typeof (Enum []));
Assert.Fail ("#D1");
} catch (NotSupportedException ex) {
// 'EnumConverter' is unable to convert '(null)'
// to 'System.Enum[]'
Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#D2");
Assert.IsNull (ex.InnerException, "#D3");
Assert.IsNotNull (ex.Message, "#D4");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (EnumConverter).Name + "'") != -1, "#D5");
Assert.IsTrue (ex.Message.IndexOf ("'(null)'") != -1, "#D6");
Assert.IsTrue (ex.Message.IndexOf ("'" + typeof (Enum []).FullName + "'") != -1, "#D7");
}
try {
converter.ConvertTo (null, CultureInfo.InvariantCulture,
"Cc", typeof (Enum []));
Assert.Fail ("#E1");
} catch (ArgumentException ex) {
// The value passed in must be an enum base or an
// underlying type for an enum, such as an Int32
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
Assert.IsNull (ex.InnerException, "#E3");
Assert.IsNotNull (ex.Message, "#E4");
Assert.IsNotNull (ex.ParamName, "#E5");
Assert.AreEqual ("value", ex.ParamName, "#E6");
}
}