简体   繁体   English

在 object 构造中传递枚举并使用其属性 + 在 C# 中表示 memory 的最佳方式

[英]Passing enum at object construction and use its attributes + best way to represent a memory in C#

I need a way to represent some memory layout for all sorts of processors and thought it might be a good idea to provide this information through the use of enums.我需要一种方法来代表各种处理器的一些 memory 布局,并认为通过使用枚举来提供这些信息可能是一个好主意。
Please suggest a better approach as I am not use using enums is the best option.请提出更好的方法,因为我不使用枚举是最好的选择。

Now my enum for a given processor ProcX is the following one:现在我对给定处理器 ProcX 的枚举如下:

public static class ProcX
{
    public enum MemorySegments
    {
        [MemSize(0x500)]
        [Description("M0 base address")]
        M0 = 0x0000,

        [MemSize(0x500)]
        [Description("M1 base address")]
        M1 = 0x0500

        ...
    }
}

As mentioned above there will be many of these definitions and they should be used in order to instantiate and allocate memory segments.如上所述,这些定义中有很多,应该使用它们来实例化和分配 memory 段。
So now, if you consider the following MemorySegment Class, see that I would like to allocate the memory within the constructor based on the enum passed to it at construction:所以现在,如果您考虑以下 MemorySegment Class,请参阅我想根据在构造时传递给它的枚举在构造函数中分配 memory:

public class MemorySegment
{
    public uint BaseAddress;

    public uint MemSize;

    ....

    public MemorySegment(Enum segments, string segName)
    {
        BaseAddress = segments(segName).....??;
        MemSize= segments(segName) .....?;

        // Then allocate mem using these values
        ...
   }
}

Now if I try to create a segment of type M0:现在,如果我尝试创建 M0 类型的段:

MemorySegment mySeg = new MemorySegment(new ProcX.MemorySegments(), "M0");    // I am passing "M0" here for this example only

Within the "MemorySegment" constructor, how can I extract the desired enum fields.在“MemorySegment”构造函数中,如何提取所需的枚举字段。 ie. IE。 For M0, we should be able to set BaseAddress to 0 and MemSize to 0x500 out of the informatino provided by the num.对于 M0,我们应该能够根据 num 提供的信息将 BaseAddress 设置为 0 并将 MemSize 设置为 0x500。

For info, here are my enum attributes:有关信息,这是我的枚举属性:

public class MemSizeAttribute : System.Attribute
{
    private int _value;
    public MemSizeAttribute(int value)
    {
        _value = value;
    }
    public int Value
    {
        get { return _value; }
    }
}

public static class EnumExtension
{
    public static int MemSize<T>(this T value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        MemSizeAttribute[] attributes = (MemSizeAttribute[])fi.GetCustomAttributes(typeof(MemSizeAttribute), false);

        if (attributes.Length > 0)
            return attributes[0].Value;
        else
            return 0;
    }

    public static string Description<T>(this T value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}

Finally, how could I cycle through each element of the enum one by one?最后,我怎样才能一个一个地循环遍历枚举的每个元素? ie. IE。 If I wanted to instantiate all the segments listed in this enum one by one (from within a loop).如果我想一一实例化此枚举中列出的所有段(从循环内)。

"best" is always subjective; “最好”总是主观的; storing things as an enum is OK , but having to look everything up via attributes is pretty inefficient and allocatey.将事物存储为enum可以的,但是必须通过属性查找所有内容是非常低效和分配的。 Frankly, if this was me I'd keep it simple:坦率地说,如果这是我,我会保持简单:

public static class ProcX
{
    public static class MemorySegments {
        public static MemorySegment M0 {get;}
            = new MemorySegment (500, "M0 base address", 0x0000);
        public static MemorySegment M1 {get;}
            = new MemorySegment (500, "M1 base address", 0x0500);
        // ...
    }
}
public sealed class MemorySegment {
    public int Size {get; }
    public string Description {get;}
    public int BaseAddress {get;}
    internal MemorySegment(int size, string description, int baseAddress) {
        Size = size;
        Description = description;
        BaseAddress = baseAddress;
    }
}

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

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