有没有办法引用继承抽象类的类(即Type
)?引用抽象类中的inherting类
class abstract Monster
{
string Weakness { get; }
string Vice { get; }
Type WhatIAm
{
get { /* somehow return the Vampire type here? */ }
}
}
class Vampire : Monster
{
string Weakness { get { return "sunlight"; }
string Vice { get { return "drinks blood"; } }
}
//somewhere else in code...
Vampire dracula = new Vampire();
Type t = dracula.WhatIAm; // t = Vampire
对于那些谁是好奇...我在做什么:我想知道什么时候我的网站最后公布。 .GetExecutingAssembly
完美工作,直到我把我的解决方案的DLL。之后,BuildDate
始终是实用程序dll的最后生成日期,而不是网站的dll。
namespace Web.BaseObjects
{
public abstract class Global : HttpApplication
{
/// <summary>
/// Gets the last build date of the website
/// </summary>
/// <remarks>This is the last write time of the website</remarks>
/// <returns></returns>
public DateTime BuildDate
{
get
{
// OLD (was also static)
//return File.GetLastWriteTime(
// System.Reflection.Assembly.GetExecutingAssembly.Location);
return File.GetLastWriteTime(
System.Reflection.Assembly.GetAssembly(this.GetType()).Location);
}
}
}
}
或者,更好的,只是用gettype()。 – 2010-10-29 20:20:25
还要注意,GetType()将获得您正在使用的任何类型的实际类型,即使您已将其转换为其他类型(当然也适用于参考类型)。所以如果你有像IMonster这样的接口,你可以使用IMonster.GetType()来查看它的实际内容,同样也适用于你的Monster抽象基础。 – CodexArcanum 2010-10-29 20:25:43