简体   繁体   English

类型“T”必须是可转换的,以便在泛型类型或方法中将其用作参数“T”

[英]The type 'T' must be convertible in order to use it as parameter 'T' in the generic type or method

I have these two main classes.我有这两个主要课程。 First the FSMSystem class:首先是 FSMSystem 类:

public class FSMSystem<T> : MonoBehaviour where T : FSMSystem<T>
{
    private T m_Owner = default(T);

    protected FSMState<T> currentState;

    private Dictionary<int, FSMState<T>> m_states;

    public FSMSystem(T owner)
    {
        m_Owner = GameObject.FindObjectOfType(typeof(T)) as T; //owner;
        m_states = new Dictionary<int, FSMState<T>>();
    }

    protected void AddState( FSMState<T> state )
    {
        m_states.Add( state.GetStateID(), state );
    }
}

And the second class, FSMState:第二个类,FSMState:

public abstract class FSMState<T>
{   
    public abstract int GetStateID();

    public abstract void OnEnter (FSMSystem<T> fsm, FSMState<T> prevState);
    public abstract void OnUpdate (FSMSystem<T> fsm);
    public abstract void OnExit (FSMSystem<T> fsm, FSMState<T> nextState);
}

It leads to the following error:它导致以下错误:

error CS0309: The type ' T ' must be convertible to ' FSMSystem<T> ' in order to use it as parameter ' T ' in the generic type or method ' FSMSystem<T> '错误 CS0309:类型“ T ”必须可转换为“ FSMSystem<T> ”,以便将其用作泛型类型或方法“ FSMSystem<T> ”中的参数“ T

Can someone tell me how to resolve this?有人可以告诉我如何解决这个问题吗? I see many other posts similar to this one but I'm not seeing the relationship.我看到许多其他帖子与此类似,但我没有看到这种关系。

The T of FSMState must also be constrained, otherwise it cannot be used as the T of FSMSystem - which has constraints placed on it ( T : FSMSystem<T> ).所述TFSMState也必须限制,否则就不能用作TFSMSystem -其具有置于其上的约束( T : FSMSystem<T>

If you would have provided the line number of the compiler error, I suspect it would point to the methods OnEnter , etc.如果您提供了编译器错误的行号,我怀疑它会指向OnEnter等方法。

It's big help to me.对我帮助很大。 thank you all.谢谢你们。 I resolved the problem.我解决了这个问题。 I misunderstood about 'where' syntax.我误解了“where”语法。

Here is the revised version that works.这是有效的修订版本。

public class FSMSystem<T> : MonoBehaviour where T : FSMSystem<T>
{
    private T m_Owner = default(T);

    protected FSMState<T> currentState;

    private Dictionary<int, FSMState<T>> m_states;

    public FSMSystem()
    {
        m_Owner = this as T;
        m_states = new Dictionary<int, FSMState<T>>();
    }

    protected void AddState( FSMState<T> state )
    {
        m_states.Add( state.GetStateID(), state );
    }
}


public abstract class FSMState<T> where T : FSMSystem<T>
{
    public abstract int GetStateID();

    public abstract void OnEnter (T fsm, FSMState<T> prevState);
    public abstract void OnUpdate (T fsm);
    public abstract void OnExit (T fsm, FSMState<T> nextState);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为了在通用类型或方法“ QueryAPI.Query”中将其用作参数“ T”,类型“ T”必须是引用类型。 <T> () - The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'QueryAPI.Query<T>() 该类型必须是可转换的,以便将其用作泛型类中的参数 - The type must be convertible in order to use it as parameter in the generic class 为了在通用类型或方法中将其用作参数“ T”,类型“字符串”必须为非空值类型 - The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 该类型必须是引用类型,才能在通用类型或方法中将其用作参数“ T” - The type must be a reference type in order to use it as parameter 'T' in the generic type or method TOut类型必须是引用类型,才能在通用方法类型中用作参数T - The Type TOut must be a reference type in order to use it as a parameter T in the generic type of method 错误注入类型“ T”必须转换为“ T”才能在通用方法中将其用作参数“ T” - Error ninject the type “T” must be convertable to “T”in order to use it as a parameter “T” in generic method 该类型(我的类)必须为非空类型,以便在通用方法中用作参数“ T” - the type (my class) must be non-nullable type in order to use as a parameter 'T' in a generic method T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method 类型“ T1”必须是不可为空的值类型,以便在通用类型或方法“ System.Nullable”中将其用作参数“ T” <T> &#39; - The type 'T1' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>' 类型&#39;T&#39;必须是非可空值类型,以便在泛型类型或方法&#39;System.Nullable中将其用作参数&#39;T&#39; <T> “ - The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM