C# TypeConverter long 到枚举类型在 ChangeType 上失败
我对 C# 和 .NET 相当陌生 - 我正在尝试从整数到枚举的转换。转换必须可以通过 ChangeType 执行(在下面的演示之外,这是固定的,因为它在数据绑定框架内)并且从我读到的内容来看,它应该与我正在做的事情一起工作,但是我得到一个异常,如果我放置我的转换类的函数中的断点,没有任何内容被调用。
提前致谢! -马修。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace csharptest
{
class Program
{
[TypeConverter(typeof(UnitEnumConverter))]
public enum LengthUnits
{
METRES,
FEET
};
public class UnitEnumConverter : EnumConverter
{
public UnitEnumConverter(System.Type type)
: base(type.GetType())
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(Int64)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is Int64)
{
return (LengthUnits)(Int64)value;
}
return base.ConvertFrom(context, culture, value);
}
}
static void Main(string[] args)
{
LengthUnits units = new LengthUnits();
long x = 1;
units = (LengthUnits)System.Convert.ChangeType(x, typeof(LengthUnits));
}
}
}
I'm fairly new to C# and .NET - I'm trying to get conversion to work from an integer to an enum. The conversion must be performable by ChangeType (outside of my demo below this is fixed as it's within the data binding framework) and from what I've read it should work with what I'm doing, but I get an exception and if I place breakpoints in the functions of my conversion class, nothing ever gets called.
Thanks in advance! -Matthew.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace csharptest
{
class Program
{
[TypeConverter(typeof(UnitEnumConverter))]
public enum LengthUnits
{
METRES,
FEET
};
public class UnitEnumConverter : EnumConverter
{
public UnitEnumConverter(System.Type type)
: base(type.GetType())
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(Int64)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is Int64)
{
return (LengthUnits)(Int64)value;
}
return base.ConvertFrom(context, culture, value);
}
}
static void Main(string[] args)
{
LengthUnits units = new LengthUnits();
long x = 1;
units = (LengthUnits)System.Convert.ChangeType(x, typeof(LengthUnits));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从以前的答案开始
Convert.ChangeType
不会费心查看TypeConverter
所以这没有帮助。使用Reflector
来查看Convert.ChangeType
它看起来不起作用。它有一个可以转换成的静态地图。如果它不在该列表中,它将不会尝试进行转换。这很有趣,因为直接将 int 或 long 转换为枚举就可以了。我不确定您正在使用哪个绑定框架,但它会沿着这条枚举路线走下去似乎很奇怪。
很抱歉我无法提供更多帮助。
Starting over from previous answers
Convert.ChangeType
will not bother looking at theTypeConverter
so that is no help. UsingReflector
to look atConvert.ChangeType
it just looks like it will not work. It has a static map of things that it can convert to. If it isn't in that list it will not try and convert. This is funny because a straight cast of an int or a long to your enum will just work.I'm not sure which binding framework you are using but it seems odd that it would go down this route for enums.
I'm sorry I couldn't be of more help.