gpt4 book ai didi

c# - TypeConverter 可以用于构造函数参数吗

转载 作者:太空狗 更新时间:2023-10-29 23:13:29 24 4
gpt4 key购买 nike

我正在尝试编写这样的标记扩展:

[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}" />

错误:

The TypeConverter for type "Length" does not support converting from string.

TypeConverter 工作如果我:

  • 将其放在 Value 属性上并具有默认构造函数。
  • 用属性修饰 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 可以用于构造函数参数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com