Bra*_*rad 3 .net c# inheritance
有没有办法引用Type继承抽象类的类(即)?
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
Run Code Online (Sandbox Code Playgroud)
对于那些好奇的人......我在做什么:我想知道我的网站上次发布的时间..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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用该GetType()方法.它是虚拟的,所以它会表现多态.
Type WhatAmI {
get { return this.GetType(); }
}
Run Code Online (Sandbox Code Playgroud)