我在将C#中的属性实现为设计器类型时遇到了一些小问题。基本上,我有多个实现接口的具体类型。我想在设计时选择哪种混凝土类型。
using System;
using System.ComponentModel;
using System.Drawing;
/// <summary>
/// implement a summarizable type
/// </summary>
public interface ISummarizable
{
/// <summary>
/// Summarize along rows
/// </summary>
/// <param name="rawdata"></param>
/// <returns></returns>
double[] Summarize(double[,] rawdata);
}
/// <summary>
/// calculate by mean value
/// </summary>
public class MeanSummary : ISummarizable
{
public double[] Summarize(double[,] rawdata)
{
double[] result = new double[rawdata.GetLength(0)];
for (int n = 0; n < result.Length; n++)
{
double value = 0;
for (int j = 0; n < rawdata.GetLength(1); j++)
value += rawdata[n, j] / rawdata.GetLength(1);
result[n] = value;
}
return result;
}
}
/// <summary>
/// Calculate by max value
/// </summary>
public class MaxSummary : ISummarizable
{
public double[] Summarize(double[,] rawdata)
{
double[] result = new double[rawdata.GetLength(0)];
for (int n = 0; n < result.Length; n++)
{
double value = double.MinValue; // guaranteed to change at least once
for (int j = 0; n < rawdata.GetLength(1); j++)
value = Math.Max(value, rawdata[n, j]);
result[n] = value;
}
return result;
}
}
/// <summary>
/// type converter
/// </summary>
public class SummaryTypeConverter : System.ComponentModel.TypeListConverter
{
public override bool CanConvertFrom(System.ComponentModel.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(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
return value.ToString();
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if(value is ISummarizable)
{
return value.GetType().Name;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
/// <summary>
/// constructor w/ initializer
/// </summary>
public SummaryTypeConverter() :
base(new Type[] { typeof(MeanSummary), typeof(MaxSummary) })
{
}
}
public class MyControl : System.Windows.Forms.Control
{
protected ISummarizable _summary = new MaxSummary();
[TypeConverter(typeof(SummaryTypeConverter))]
public ISummarizable Summary { get { return _summary; } set { _summary = value; } }
}在属性查看器中,我为“汇总”属性提供了一个不错的下拉列表,其中包含了我的两种具体类型。但是在选择一个时,错误是
Object of type System.String cannot be converted to type Example.ISummarizable我尝试在ConvertFrom和ConvertTo中实现ConvertFrom和ConvertTo,但没有成功,并尝试使用第二个Visual实例对其进行调试,但我甚至无法捕获此错误。有什么想法吗?
发布于 2015-05-18 05:55:05
在您的ConvertFrom实现中,在这种情况下,您会收到一个有意义的字符串(例如。),您需要返回实际类型的实例,而不是调用基方法(正如Carsten所说)。
您可以尝试使用反射(查找具有该名称的类型,检查它是否为ISummarizable,调用构造函数)或使用开关语句。
通过反射,您的代码将是:
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
var source = (string)value;
var sourceType = Type.GetType(source);
if (sourceType != null && typeof(ISummarizable).IsAssignableFrom(sourceType))
{
var constructor = sourceType.GetConstructor(Type.EmptyTypes);
var sourceInstance = constructor.Invoke(new object[0]);
return sourceInstance;
}
}
return base.ConvertFrom(context, culture, value);
}https://stackoverflow.com/questions/30295193
复制相似问题