简体   繁体   中英

C# Struct / Lookup Table

Say I have a struct declared like the following:

public struct Test
{
    public static int Width = 5;
    ...
    public static int[] Value = new int[1]{ 0 };
}

Now what I want to do is call this from within another struct, but I have to clue how. What I'm trying to do would (in my mind) look like the following:

public struct AnotherStruct
{
    public (type of struct) this[int key]
    {
        get
        {
            switch(key)
            {
                case 1:
                    return (another struct);
                default:
                    return null;
            }
        }
    }
}

My end goal is that I want to use code that looks like the following, without having to create an instance of the object:

structobject s = new AnotherStruct[5];

So this 'lookup table' will be created in another project and built, then called as a dll from my main project. Since I'm building the dll elsewhere and calling it, I'm hoping that I can get the dll loaded into memory once, and then I can just reference that memory from my main project. Then I'll have one allocated portion of memory and my code will just reference it, avoiding the need to create individual instances of this lookup table (thus avoiding the time overhead it takes to allocate the memory and store the new instance). The time I'd save would be hugely beneficial in the long run, so I'm hoping I can get this to work somehow.

I hope this isn't too confusing, but let me know if any clarification is needed.

Edit This is being used on a website, so really I need an object that persists across all connections and is created once when the code is initially loaded. Same idea, but maybe that will make for a simpler solution?

Solution #1. Using a common interface for all the structures and a dictionary collection

public interface IStr { }

public struct St1 : IStr
{
    public static int ID = 1;
}
public struct St2 : IStr
{
    public static int ID = 2;
}

public class StructFactory : System.Collections.ObjectModel.KeyedCollection<int, IStr>
{
    public static StructFactory Default = new StructFactory();
    protected override int GetKeyForItem(IStr item)
    {
        FieldInfo finfo = item.GetType().GetField("ID", 
            BindingFlags.Static | BindingFlags.Public);

        return (int)finfo.GetValue(item);
    }

    public StructFactory()
    {
        Add(new St1());
        Add(new St2());
    }
}

class Program
{
    static void Main(string[] args)
    {
        St1 x = (St1)StructFactory.Default[1];
        St2 y = (St2)StructFactory.Default[2];
    }
}

The syntax you use above won't work since it means "create an array of AnotherStruct with five elements in it." As mentioned in a comment, however, you really should look into using a factory pattern.

However, if you really want to use the pattern above, you could change it up slightly. Have your AnotherStruct array hold Type instances of each of your structs. Then, your "creation" line would look more like:

structobject s = (structobject)Activator.CreateInstance(AnotherStruct[5]);

You can use reflection on the Assembly (since you are wrapping it in a DLL) to get those Type objects.

And finally, unless you have a really good reason for using struct (and understand all of the nuances, of which there are several), stick with class .

Solution #2. Forgo the whole ID idea and just use the structure type and generics.

public struct St1 
{
}
public struct St2 
{
}

public class Factory<T>
    where T : struct
{
    static T _new = new T(); //cached copy of structure

    public static T New { get { return _new; } }        
}


class Program
{
    static void Main(string[] args)
    {
        St1 x1 = Factory<St1>.New;
        St1 x2 = Factory<St1>.New;
        St1 x3 = Factory<St1>.New;
        St2 y1 = Factory<St2>.New;
        St2 y2 = Factory<St2>.New;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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