简体   繁体   English

泛型方法中的 C# 结构

[英]C# struct in generic method

I try to do something like this:我尝试做这样的事情:

class Program
    {
        public struct frst
        {
            public short Oversize_FrontInch;
            public short Oversize_RearInch;
            public short Oversize_RightInch;
            public short Oversize_LeftInch;
            public short HeightInch;
        }
        public struct sec
        {
            public short Oversize_FrontInch;
            public short Oversize_RearInch;
            public short Oversize_RightInch;
            public short Oversize_LeftInch;
        }
        public void DoSmth<T>() where T:struct
        { 
            T str = new T();
            str.Oversize_FrontInch = (short)2;

        }
    }

but i get "t does not contain a definition for ..." error is there any way do do this?但我得到“t不包含...的定义”错误有没有办法做到这一点? Important thing: for the sake of rest code it is have to be struct.重要的事情:为了休息代码,它必须是结构。

It would be great to understand more what you're trying to achieve with these seemingly unrelated structs but here's a version which compiles and runs and keeps sec and frst as structs:使用这些看似不相关的结构来了解更多您想要实现的目标会很棒,但这是一个编译和运行并将secfrst保留为结构的版本:

DoSmth<sec>();
DoSmth<frst>();

void DoSmth<T>() where T: struct, IOversize
{ 
    T str = default;
    str.Oversize_FrontInch = (short)2;
    Console.WriteLine(str.GetType());
}


public interface IOversize 
{
    short Oversize_FrontInch {get; set;}
}

public struct sec: IOversize
{
    public short Oversize_FrontInch {get; set;}
}        

public struct frst: IOversize
{
    public short Oversize_FrontInch {get; set;}
    public short HeightInch {get; set;}
}

This prints:这打印:

sec
frst

I am guessing you want something like this.我猜你想要这样的东西。

Struct cannot inherit so you code cannot do what you want.结构不能继承,所以你的代码不能做你想做的事。

You can instead use a base class and inherit data class from it.您可以改为使用基类并从中继承数据类。

But be aware that you need to inherit a default constructor in order to create a dataclass in a generic function.但请注意,您需要继承默认构造函数才能在泛型函数中创建数据类。

This is a simple example for you to play with, you can edit and polish it as you want.这是一个简单的示例供您使用,您可以根据需要对其进行编辑和润色。

using System;

public class HelloWorld
{
    public class baseclass{
        public short Oversize_FrontInch;
        public short Oversize_RearInch;
        public short Oversize_RightInch;
        public short Oversize_LeftInch;
    }
    public class frst:baseclass
    {
        public short HeightInch;
    }
    public class sec:baseclass 
    { 
    }
    
    public static void DoSmth<T>() where T:baseclass, new()
    { 
        T str = new T();
        str.Oversize_FrontInch = (short)2;
        Console.WriteLine(str.Oversize_FrontInch);
    }
    
    public static void Main(string[] args)
    { 
        DoSmth<frst>();
    }
}

Edit: I saw some people suggesting using interface with struct.编辑:我看到有人建议使用带有结构的接口。 It is possible.有可能的。 However, it will box all of your value, create performance issue and create unreadable code.但是,它将包装您的所有价值,产生性能问题并创建不可读的代码。 I would not recommanded it.我不会推荐它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM