c# – 可以将TypeConverter用于构造函数参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 可以将TypeConverter用于构造函数参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2709字,纯文字阅读大概需要4分钟。
内容图文
![c# – 可以将TypeConverter用于构造函数参数](/upload/InfoBanner/zyjiaocheng/825/a70be7b215b7484e832132afc0ff62a8.jpg)
我正在尝试写一个像这样的markupextension:
[MarkupExtensionReturnType(typeof(Length))]
public class LengthExtension : MarkupExtension
{
// adding the attribute like this compiles but does nothing.
public LengthExtension([TypeConverter(typeof(LengthTypeConverter))]Length value)
{
this.Value = value;
}
[ConstructorArgument("value")]
public Length Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this.Value;
}
}
要像这样使用:
<Label Content="{units:Length 1 mm}" />
Errs:
The TypeConverter for type “Length” does not support converting from string.
如果我:TypeConverter工作:
>将它放在Valueproperty上并拥有默认的ctor.
>使用属性装饰Length类型.
虽然这可能是x / y,但我不想要任何这些解决方案.
这是转换器的代码:
public class LengthTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var text = value as string;
if (text != null)
{
return Length.Parse(text, culture);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Length && destinationType != null)
{
var length = (Length)value;
if (destinationType == typeof(string))
{
return length.ToString(culture);
}
else if (destinationType == typeof(InstanceDescriptor))
{
var factoryMethod = typeof(Length).GetMethod(nameof(Length.FromMetres), BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(double) }, null);
if (factoryMethod != null)
{
var args = new object[] { length.metres };
return new InstanceDescriptor(factoryMethod, args);
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
解决方法:
从MSDN, Applying the TypeConverterAttribute(强调我的):
In order for your custom type converter to be used as the acting type
converter for a custom class by a XAML processor, you must apply the
.NET Framework attribute TypeConverterAttribute to your class
definition…
和
You can also provide a type converter on a per-property basis. Instead
of applying a .NET Framework attribute TypeConverterAttribute to the
class definition, apply it to a property definition…
没有提及任何其他地方来应用该属性.所以很有可能回答你的问题
No, a TypeConverter can not be used for a constructor argument.
内容总结
以上是互联网集市为您收集整理的c# – 可以将TypeConverter用于构造函数参数全部内容,希望文章能够帮你解决c# – 可以将TypeConverter用于构造函数参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。