C++ to C# porting

C++ to C# porting

本文关键字:porting to C++      更新时间:2023-10-16

如何将这段代码(c ++)移植到c#?

template <class entity_type>
class State {
public:
    virtual void Enter(entity_type*) = 0;
    virtual void Execute(entity_type*) = 0;
    virtual void Exit(entity_type*) = 0;
    virtual ~State() { }
};

假设这确实是一个纯粹抽象的基类,它看起来像这样:

interface State<T>
{
    void Enter(T arg);
    void Execute(T arg);
    void Exit(T arg);
};

不过,确切的参数传递约定很尴尬。如果不知道你想做什么,就很难确切地说出你应该在 C# 中做什么。可能,void FunctionName(ref T arg)可能更合适。

一些类似的东西:

interface State<T> : IDisposable
{
    void Enter(T t);
    void Execute(T t);
    void Exit(T t);
}
public abstract class State<entity_type>
    {
        public abstract void Enter(entity_type obj);
        public abstract void Execute(entity_type obj);
        public abstract void Exit(entity_type obj);
    }

这似乎:D有效

你可以这样写

abstract class State<T> : IDisposable where T : EntityType
{
    public abstract void Enter(T t);
    public abstract void Execute(T t);
    public abstract void Exit(T t);
    public abstract void Dispose();
}

将 T 修复为实体类型类。