Best way to refer to my own type

前端 未结 5 1904
轻奢々
轻奢々 2021-01-03 06:02
abstract class A where T:A
{
    public event Action Event1;
}

class B : A
{
    //has a field called Action Event1;
}
         


        
5条回答
  •  独厮守ぢ
    2021-01-03 06:40

    My most recent question was marked as a duplicate of this one. I totally agree on that matter. So I came here to take a look at the answers and to read Eric's post on that (very interesting indeed). You can not enforce this at compile time with the type system but you can do this at runtime. The way I implemented this is:

    abstract class FooBase
    {
        protected FooBase()
        {
            if (typeof(T) != GetType())
            {
                throw new InvalidOperationException();
            }
        }
    }
    

    By doing this we can plant the seed of an evil dog, but that dog will be aborted at runtime.

提交回复
热议问题