jeudi 5 octobre 2017

Is this considered reflection?

I had a job interview today and I was asked, if the code below is a good example of using reflection in C#:

public abstract class Level{

    public string LevelID { get; private set;}
    public int LevelNumber {
        get{
            return int.Parse(LevelID.Substring(5).ToString());
        }
    }

    public Level(){
        this.LevelID = GetType().ToString();
    }

}

I assume the use of the code above would be:

class Level32 : Level{
    // call base class constructor...
}

and then

Level32 level = new Level32(); 
int id = level.LevelNumber; // would print 32.

I think the guy meant this line: this.LevelID = GetType().ToString();

My answer way quite brutal and I said "there is no reflection in that code" and he looked at me like I was a "Hello World" kid.

As good as I know Java, calling SomeClass.class.getName() does not use any of the 'reflective' packages, so it doesn't use reflection at all. I thought that C# is built that way too.

Am I dumb, or he is?





Aucun commentaire:

Enregistrer un commentaire