与结构的接口,通过使用泛型进行引用

发布于 2024-08-25 04:03:04 字数 1115 浏览 16 评论 0原文

对不起各位!我非常喜欢代码!我忘记添加编译器错误。

这是新版本的代码简化版!

这是错误:

错误 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 技术交流群。

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

发布评论

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

评论(3

冰雪梦之恋 2024-09-01 04:03:04

在您的代码中,您没有传递一个带有结构的接口,而是传递一个实现接口的结构。这是完全不同的两件事。至于解决你的问题,我根本看不出使用结构的理由,所以我将把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.

美胚控场 2024-09-01 04:03:04

问题出在 DB.ModifyData(Data); 方法调用中。您的 Data 字段是一个结构体,结构体会按值传递给任何方法,这意味着每次调用方法时都会复制该结构体创建并传递给该方法。因此,实际上您的 ModifyData 方法正在修改在方法调用后立即丢弃的 Periods 结构的副本。

The problem is in DB.ModifyData<Period>(Data); method call. Your Data 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 your ModifyData method is modifying a copy of the Periods structure that is thrown away just after the method call.

终陌 2024-09-01 04:03:04

结构是值类型,而不是引用类型。如果您想通过引用传递它,则必须使用 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.

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