How do you do a casting of a geric class?
I am trying the following but it doesn't work. This is the error that is giving me
CS0119 C# 'Entity' is type, which is not valid in the given context
//-----------------------------------------------------------------
//DataBase: Es un tipo génerico
public class Entity<DataBase> : MonoBehaviour where DataBase : class
{
[SerializeField] private DataBase dataBase;
}
//-----------------------------------------------------------------
//EnemyDataBase: Es un tipo específico
public class Enemy : Entity<EnemyDataBase>
{
EnemyDataBase enemyDataBase;
}
//-----------------------------------------------------------------
//DataBase: Es un tipo génerico
public class State <DataBase> where DataBase : class
{
protected Entity<DataBase> entity;
Public method(Entity<DataBase> entity)
{
}
}
//-----------------------------------------------------------------
public class MyEnemy : Enemy
{
State state;
Public void Start()
{
state = new State();
state.method( Entity<EnemyDataBase> this); // --> asi no funciona
state.method( (Entity)<EnemyDataBase> this); // --> asi no funciona
state.method( ((Entity)<EnemyDataBase>) this); // --> asi no funciona
}
}
//-----------------------------------------------------------------
Thank you very much!!
I understand that your question would be to know if your object is of the same type created, but I am missing some mental information to give you a totally clear answer, so excuse me if I don't get it right. I also don't see where you're creating the object you want to cast but you're trying to pass the same class to it
this
instead of a concrete object.If you create a Generic Type and then want to know if that created type is equal to another, you can use two operators in C#,
is
andas
the operator
is
asks if this is of a certain type, which returns antrue
orfalse
;The operator
as
tries to cast the object and if it can't, it leaves the object innull
and doesn't break your app. Otherwise, it would happen if you want to do the cast like this:(Enemy)MiObjetoCreado
;