与结构的接口,通过使用泛型进行引用
对不起各位!我非常喜欢代码!我忘记添加编译器错误。
这是新版本的代码简化版!
这是错误:
错误 1 'IWeird.DataBase.ModifyData(ref IWeird.IDataTable)' 的最佳重载方法匹配有一些无效参数
错误 2 参数“1”:无法从“ref IWeird.Periods”转换为“ref IWeird.IDataTable”
问题: 我无法通过引用传递其中包含结构的接口, 我做错了什么?
这是新的示例代码:
class PeriodsProcessor
{
public PeriodsProcessor()
{
Periods Data = new Periods();
DataBase DB = new DataBase();
Console.WriteLine(Data.Value);
DB.ModifyData(ref Data);
Console.WriteLine(Data.Value);
Console.ReadLine();
}
}
public interface IDataTable
{
string Value { get; set; }
}
public struct Periods : IDataTable
{
public string Value { get; set; }
}
public class DataBase
{
public void ModifyData(ref IDataTable data)
{
data.Value = "CHANGE";
}
}
class Program
{
static void Main(string[] args)
{
PeriodsProcessor PeriodsProcessor = new PeriodsProcessor();
}
}
Sorry guys! i am so into the code! that i forgot to put the compiler errors.
Here is a new version of the code simplified!
And this are the errors:
Error 1 The best overloaded method match for 'IWeird.DataBase.ModifyData(ref IWeird.IDataTable)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'ref IWeird.Periods' to 'ref IWeird.IDataTable'
The problem:
I can't pass by reference an interface with a struct in it,
what am I doing wrong?
Here is the new example code:
class PeriodsProcessor
{
public PeriodsProcessor()
{
Periods Data = new Periods();
DataBase DB = new DataBase();
Console.WriteLine(Data.Value);
DB.ModifyData(ref Data);
Console.WriteLine(Data.Value);
Console.ReadLine();
}
}
public interface IDataTable
{
string Value { get; set; }
}
public struct Periods : IDataTable
{
public string Value { get; set; }
}
public class DataBase
{
public void ModifyData(ref IDataTable data)
{
data.Value = "CHANGE";
}
}
class Program
{
static void Main(string[] args)
{
PeriodsProcessor PeriodsProcessor = new PeriodsProcessor();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的代码中,您没有传递一个带有结构的接口,而是传递一个实现接口的结构。这是完全不同的两件事。至于解决你的问题,我根本看不出使用结构的理由,所以我将把Periods更改为一个类。
In your code you are not passing an interface with a struct in it, you are passing a struct that implements an interface. Those are two COMPLETELY different things. As for solving your problem, I don't see a reason to be using a struct at all, so I'd change Periods to be a class.
问题出在
DB.ModifyData(Data);
方法调用中。您的Data
字段是一个结构体,结构体会按值传递给任何方法,这意味着每次调用方法时都会复制该结构体创建并传递给该方法。因此,实际上您的ModifyData
方法正在修改在方法调用后立即丢弃的Periods
结构的副本。The problem is in
DB.ModifyData<Period>(Data);
method call. YourData
field is a struct, structs are passed to any methods by value which means that each time you call a method copy of the struct is created and is passed to the method. So actually yourModifyData
method is modifying a copy of thePeriods
structure that is thrown away just after the method call.结构是值类型,而不是引用类型。如果您想通过引用传递它,则必须使用
ref
关键字。请记住,它仍然是一个值类型,并将其分配给另一个变量将创建一个新的结构。Structs are value types, not reference types. If you want to pass it by reference, you're going to have to use the
ref
keyword. Keep in mind though that it's still a value type, and assigning it to another variable is going to create a new struct.