From d0bfabe9b0056d4ace6b905a5ddc63704b09d0d1 Mon Sep 17 00:00:00 2001 From: zengwenjie <1663900244@qq.com> Date: Fri, 26 Sep 2025 12:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=BA=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E4=B8=AD=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Deedy.Testing/ExternalTypeConverter.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 DeedyDesigner/Deedy.Testing/ExternalTypeConverter.cs diff --git a/DeedyDesigner/Deedy.Testing/ExternalTypeConverter.cs b/DeedyDesigner/Deedy.Testing/ExternalTypeConverter.cs new file mode 100644 index 0000000..567de56 --- /dev/null +++ b/DeedyDesigner/Deedy.Testing/ExternalTypeConverter.cs @@ -0,0 +1,49 @@ +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + + +/* +public partial class App : Application +{ + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + // 注册外部类型的「TypeConverter」 + TypeDescriptor.AddAttributes(typeof(ExternalPerson), + new TypeConverterAttribute(typeof(ExternalPersonConverter))); + } +} +*/ + + + +// 在目标程序集中定义类型时添加转换器 +[TypeConverter(typeof(ExternalTypeConverter))] +public class ExternalType { + public ExternalType(string? value) { } +} +// 在主程序集中定义元数据类 +[TypeConverter(typeof(ExternalTypeConverter))] +public class ExternalTypeMetadata { } + +// 关联元数据与外部类型 +[MetadataType(typeof(ExternalTypeMetadata))] +public partial class ExternalTypeProxy +{ + // 通过代理类访问外部类型 + [AllowNull] + public ExternalType Target { get; set; } +} +public class ExternalTypeConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) => sourceType == typeof(string); + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) + { + return new ExternalType(value.ToString()); + } +} \ No newline at end of file