从通用方法中访问公共成员

发布于 2025-01-28 08:46:57 字数 944 浏览 2 评论 0原文

我试图在该工具箱中使用每个工具的工具箱有一些工具箱,在功能上有很大不同。因此,每个工具都需要在开始时或需要更改参数时创建和传递自己的配置尝试从通用方法访问某些配置的公共成员时出错。我有这样的东西:

public abstract class Tool
{
    public abstract void Setup<T>(T setupParams);
}

public class SomeTool : Tool
{
    public class SomeConfig
    {
        public int counter;
        public float size;
        public SomeConfig(int _counter, float _size)
        {
            counter = _counter;
            size = _size;
        }
    }

    int m_counter;
    int m_size;

    public override void Setup<SomeConfig>(SomeConfig setupParams)
    {
        m_counter = setupParams.counter;
        //'SomeConfig' does not contain a definition for 'counter' and no accessible extension
        // method 'counter' accepting a first argument of type 'SomeConfig' could be found
        // (are you missing a using directive or an assembly reference?)
    }
}

拥有构造函数或非通用设置功能有效,但是为什么我在这里做的事情会遇到错误呢?

I'm trying to have some toolbox with each tool in that toolbox being something quite different in functionality. So every tool would require its own config to be created and passed at the start, or when you need to change params, while the base class would be just the general outline of what all the tools have in common, but I'm getting an error when trying to access a public member of some config from a generic method. I have something like this:

public abstract class Tool
{
    public abstract void Setup<T>(T setupParams);
}

public class SomeTool : Tool
{
    public class SomeConfig
    {
        public int counter;
        public float size;
        public SomeConfig(int _counter, float _size)
        {
            counter = _counter;
            size = _size;
        }
    }

    int m_counter;
    int m_size;

    public override void Setup<SomeConfig>(SomeConfig setupParams)
    {
        m_counter = setupParams.counter;
        //'SomeConfig' does not contain a definition for 'counter' and no accessible extension
        // method 'counter' accepting a first argument of type 'SomeConfig' could be found
        // (are you missing a using directive or an assembly reference?)
    }
}

Having a constructor, or non generic setup function works, but why am I getting an error with what I am doing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2025-02-04 08:46:57

当您这样做时:

public abstract void Setup<T>(T setupParams)

您正在使用&lt; t&gt;来定义类型。该类型不存在。 t不是类。您可以将任何名称和编译放置:

public override void Setup<WhatEver>(WhatEver setupParams)

但是whyt是您在那里定义的一种类型。它不存在。

您可以以此形式定义类:

public abstract class Tool<T>
{
    public abstract void Setup(T setupParams);
}

public class SomeConfig
{
    public int counter;
    public float size;
    public SomeConfig(int _counter, float _size)
    {
        counter = _counter;
        size = _size;
    }
}

public class SomeTool : Tool<SomeConfig>
{

    int m_counter;
    int m_size;

    public override void Setup(SomeConfig setupParams)
    {
        m_counter = setupParams.counter;
    }
}

When you do this:

public abstract void Setup<T>(T setupParams)

You are using <T> to define the type. That type doesn't exists. T isn't a class. You can put any name and compile:

public override void Setup<WhatEver>(WhatEver setupParams)

But WhatEver is a type that you define just there. It doesn't exists.

You can define the class in this form:

public abstract class Tool<T>
{
    public abstract void Setup(T setupParams);
}

public class SomeConfig
{
    public int counter;
    public float size;
    public SomeConfig(int _counter, float _size)
    {
        counter = _counter;
        size = _size;
    }
}

public class SomeTool : Tool<SomeConfig>
{

    int m_counter;
    int m_size;

    public override void Setup(SomeConfig setupParams)
    {
        m_counter = setupParams.counter;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文