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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您这样做时:
您正在使用
&lt; t&gt;
来定义类型。该类型不存在。t
不是类。您可以将任何名称和编译放置:但是
whyt
是您在那里定义的一种类型。它不存在。您可以以此形式定义类:
When you do this:
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:But
WhatEver
is a type that you define just there. It doesn't exists.You can define the class in this form: