开发者

How do I implement a static dictionary<T> with parameters at Runtime in C#?

I have the following code:

   public static class ScraperMasterUriDetails
    {
        public static Dictionary<Guid, string> MasterUriDetails;

    }

However I've decided that I need to add an integer to the dictionary Dictionary<ScraperMasterUriDetails>, so I thought I'd add properties and a few parameters to the constructor.

But, you can't do that in C#. How do I implement what I'm trying to implement?

EDIT:

A more experienced member edited my post, so I'll leave it how it is. I don't care about using a dictionary (was just the right thing to use at the time)

Essentially I just want a list of three types of data in a structure manner, except I always want to refer to once instance of the class which stores the values, hence static. Now I've always done List of(T) like:

public class WebsiteTitles
{
    public string WebsiteId { get; set; }
    public string Keywords { get; set; }

    public WebsiteTitles(string websiteguid, string keywords)
    {
        WebsiteId = websiteguid;
        Keywords = keywords;
    }

    public WebsiteTitles()
    {
    }
}

And then done the following

           List<WebsiteTitles> _siteTitles = new List<WebsiteTitles>();
            _siteTitles.Add(new WebsiteTitles("blah", "keyword")); 

However in this scenario I want like something similar to the above, but static (don't want to be creating instances etc. I really appreciate all the suggestions, hence why I开发者_运维问答 edited my post to provide more information.

As a note, I'll probably want to use LINQ to extract some records e.g. get record where guid == guid etc. That's about all I'll use it for.


Singleton.

http://en.wikipedia.org/wiki/Singleton_pattern


A static constructor is called automatically, not by you. There is no way for it to know what parameters to pass in.

So, either

  1. Set the properties to values in the constructor
  2. Set them later with another method you add e.g. Initialize(int a, ...)


Why not just use the collection initializer syntax? http://msdn.microsoft.com/en-us/library/bb384062.aspx

public static class ScraperMasterUriDetails
{
    public static List<WebsiteTitles> MasterUriDetails = new List<WebsiteTitles>()
    {
        new WebsiteTitles()
        {
            WebsiteId = Guid.NewGuid().ToString(),
            Keywords = "programming, fish, popsicles, nihilism",
        },
    };
}

If you need to look up a website by ID, then by all means use a dictionary:

public static class ScraperMasterUriDetails
{
    public static Dictionary<Guid, string> MasterUriDetails = new Dictionary<Guid, string>()
    {
        { Guid.NewGuid(), "programming, fish, popsicles, nihilism" },
        { new Guid("abcdef" /* etc */), "ankles, sprocket, glucose, the moon" },
    };
}

Before Edit:

Please note that a dictionary isn't a list.

However, you can of course add values to a static member at any time. It doesn't have to be defined at compile time:

public class SomeOtherClass
{
    public void SomeNonStaticMethod(Guid key, string subUrl)
    {
        ScraperMasterUriDetails.MasterUriDetails[key] = "http://www.helpmeimstuckinsideasadnsserver.com/" + subUrl;
    }
}

If you need more information than this, please describe the structure you used to "add an int". If you are having problems defining that structure, please explain what you want your dictionary to do. You pass it a GUID, and get back what information?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜