我有这些课程:
public class FilterBackOfficeDiscountFilterType
{
public FilterBackOfficeDiscountFilterType();
public FilterBackOfficeDiscountFilterType(string filterValue = null, string filterType = null);
[JsonProperty(PropertyName = "filterType")]
public string FilterType { get; set; }
[JsonProperty(PropertyName = "filterValue")]
public string FilterValue { get; set; }
}
我的:
public abstract class Filter<T>
where T : struct
{
public T Type { get; set; }
public string Value { get; set; }
}
和
public class DiscountFilter : Filter<DiscountFilterType>
{
}
我想以显式或隐式的方式将DiscountFilter
投射到FilterBackOfficeDiscountFilterType
。
所以我添加了运算符方法:
public class DiscountFilter : Filter<DiscountFilterType>
{
public static implicit operator FilterBackOfficeDiscountFilterType(DiscountFilter filter)
=> new FilterBackOfficeDiscountFilterType(filterType: filter.Type.ToString(), filterValue: filter.Value);
}
但它不能编译,因为:
Cannot implicitly convert type 'Filter<DiscountFilterType>' to 'FilterBackOfficeDiscountFilterType'
我怎么做?
DiscountFilterType
是一个枚举。
FilterCreator.Create是:
public class DiscountFilterCreator : FilterCreator<DiscountFilterType>
{
public override Filter<DiscountFilterType> Create(DiscountFilterType type, string value)
=> new DiscountFilter {Type = type, Value = string.IsNullOrWhiteSpace(value) ? null : value};
}
它源于:
public abstract class FilterCreator<T>
where T : struct
{
public abstract Filter<T> Create(T type, string value);
}
最后Filter
是:
public abstract class Filter<T>
where T : struct
{
public T Type { get; set; }
public string Value { get; set; }
}
修改 重现你必须做的这个问题
DiscountFilter filter = new DiscountFilterCreator().Create(DiscountFilterType.normal, "wow");
修改
DiscountFilter x = this.FilterCreator.Create(DiscountFilterType.BrandId, brandId);
FilterBackOfficeDiscountFilterType y = x;
修改 它有效:
DiscountFilter x = (DiscountFilter)this.FilterCreator.Create(DiscountFilterType.BrandId, brandId);
FilterBackOfficeDiscountFilterType y = x;
但我想这样做:
FilterBackOfficeDiscountFilterType x = this.FilterCreator.Create(DiscountFilterType.BrandId, brandId);
所以我也需要这种隐式转换:
public class DiscountFilter : Filter<DiscountFilterType>
{
public static implicit operator DiscountFilter(Filter<DiscountFilterType> filter)
=> new DiscountFilter {Value = filter.Value};
public static implicit operator FilterBackOfficeDiscountFilterType(DiscountFilter filter)
=> new FilterBackOfficeDiscountFilterType(filterType: filter.Type.ToString(), filterValue: filter.Value);
}
但是不允许从派生类到基类的隐式转换!
DiscountFilter.implicit operator DiscountFilter(Filter<DiscountFilterType>): user-defined conversions to or from a base class are not allowed
答案 0 :(得分:3)
(隐式)用户定义的从基类到派生类的转换是一个坏主意,因此不支持此处已解释过的语言:User-defined conversion operator from base class
作为解决问题的方法,我建议您按照以下方式调整基本类型:
public abstract class FilterCreator<TFilter, TFilterType>
where TFilter : Filter<TFilterType>
where TFilterType : struct
{
public abstract TFilter Create(TFilterType type, string value);
}
public abstract class Filter<T>
where T : struct
{
public T Type { get; set; }
public string Value { get; set; }
}
然后您可以将DiscountFilterCreator实现为
public class DiscountFilterCreator : FilterCreator<DiscountFilter, DiscountFilterType>
{
public override DiscountFilter Create(DiscountFilterType type, string value)
=> new DiscountFilter { Type = type, Value = string.IsNullOrWhiteSpace(value) ? null : value };
}
使用此方法,您将不再需要强制转换,因为返回的类型是预期的类型。
不确定这正是您想要的。 C#标准的代码看起来非常抽象,可能有更好的设计满足您的需求。
编辑:正如旁注所示,此实现使FilterCreator
非常接近无用。 Filter<T>
的用处似乎也值得怀疑 - 您是否曾声明Filter<T>
类型的变量或通用约束?
答案 1 :(得分:1)
要重现您的问题,您尝试将基类泛型类强制转换为派生类。
类似这样的事情
DiscountFilter filter = new DiscountFilterCreator().Create(DiscountFilterType.normal, "wow");
解决您必须转换为派生类的问题
DiscountFilter filter =(DiscountFilter) new DiscountFilterCreator().Create(DiscountFilterType.normal, "wow");