转换 类 并设置公共值
Casting Classes and Setting Common Values
我们有一系列的classes,它们来源于一个共同的class:
public class OurBaseClass
{
public string StatusMessage { get; set;}
[other properties]
}
public class ProcessClass : OurBaseClass
{
public string SomeProcessInformation { get; set;}
public string SomeMoreProcessInformation { get; set;}
[other properties]
}
然后我们尝试创建一个函数来设置 SpecificProcessClass 的基本属性,并且当前有(不起作用):
public object DefaultResponse(string messageText)
{
return new OurBaseClass
{
StatusMessage = messageText,
[other properties] = ...
}
};
我们的意图是
ProcessClass resp = (ProcessClass) DefaultResponse("Some Message");
resp.SomeProcessInformation = "";
resp.SomeMoreProcessInformation = "";
[other properties] = ...
return resp;
推理,是为了尽量减少重复编码的数量,使功能易于阅读(肉眼);这会引发以下错误。
System.InvalidCastException: Unable to cast object of type 'OurBaseClass' to type 'ProcessClass'
虽然对结果并不完全感到惊讶,因为 ProcessClass 是从 OurBaseClass 派生的,但我认为可以这样做,只是不确定如何...
如何在基 class 中创建一个方法来设置公共属性,然后在子 class 中有一个可以重写的虚方法,每个方法都调用基方法.
public class OurBaseClass
{
public string StatusMessage { get; set;}
// other properties
protected void BaseInit()
{
// Set common properties here...
}
public virtual void Init()
{
BaseInit();
}
}
public class ProcessClass : OurBaseClass
{
public string SomeProcessInformation { get; set;}
public string SomeMoreProcessInformation { get; set;}
public override void Init()
{
BaseInit();
// Set specific properties here...
}
}
您不能从派生程度较低的类型强制转换为派生程度较高的类型,您需要首先创建派生程度较高的类型。
一种类似于您当前代码的解决方案是使用泛型:
public T DefaultResponse<T>(string messageText)
where T : OurBaseClass, new()
{
return new T
{
StatusMessage = messageText,
};
}
where
约束将 T
限制为 OurBaseClass
或派生类型,而 new()
意味着 T
必须具有无参数构造函数。您可以阅读更多关于它们的信息 in the documentation
可以这样使用:
ProcessClass resp = DefaultResponse<ProcessClass>("Some Message");
resp.SomeProcessInformation = "";
resp.SomeMoreProcessInformation = "";
我们有一系列的classes,它们来源于一个共同的class:
public class OurBaseClass
{
public string StatusMessage { get; set;}
[other properties]
}
public class ProcessClass : OurBaseClass
{
public string SomeProcessInformation { get; set;}
public string SomeMoreProcessInformation { get; set;}
[other properties]
}
然后我们尝试创建一个函数来设置 SpecificProcessClass 的基本属性,并且当前有(不起作用):
public object DefaultResponse(string messageText)
{
return new OurBaseClass
{
StatusMessage = messageText,
[other properties] = ...
}
};
我们的意图是
ProcessClass resp = (ProcessClass) DefaultResponse("Some Message");
resp.SomeProcessInformation = "";
resp.SomeMoreProcessInformation = "";
[other properties] = ...
return resp;
推理,是为了尽量减少重复编码的数量,使功能易于阅读(肉眼);这会引发以下错误。
System.InvalidCastException: Unable to cast object of type 'OurBaseClass' to type 'ProcessClass'
虽然对结果并不完全感到惊讶,因为 ProcessClass 是从 OurBaseClass 派生的,但我认为可以这样做,只是不确定如何...
如何在基 class 中创建一个方法来设置公共属性,然后在子 class 中有一个可以重写的虚方法,每个方法都调用基方法.
public class OurBaseClass
{
public string StatusMessage { get; set;}
// other properties
protected void BaseInit()
{
// Set common properties here...
}
public virtual void Init()
{
BaseInit();
}
}
public class ProcessClass : OurBaseClass
{
public string SomeProcessInformation { get; set;}
public string SomeMoreProcessInformation { get; set;}
public override void Init()
{
BaseInit();
// Set specific properties here...
}
}
您不能从派生程度较低的类型强制转换为派生程度较高的类型,您需要首先创建派生程度较高的类型。
一种类似于您当前代码的解决方案是使用泛型:
public T DefaultResponse<T>(string messageText)
where T : OurBaseClass, new()
{
return new T
{
StatusMessage = messageText,
};
}
where
约束将 T
限制为 OurBaseClass
或派生类型,而 new()
意味着 T
必须具有无参数构造函数。您可以阅读更多关于它们的信息 in the documentation
可以这样使用:
ProcessClass resp = DefaultResponse<ProcessClass>("Some Message");
resp.SomeProcessInformation = "";
resp.SomeMoreProcessInformation = "";