如何实现静态字典在 C# 运行时使用参数?

发布于 2024-09-14 01:52:29 字数 1169 浏览 2 评论 0原文

我有以下代码:

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

    }

但是我决定需要向字典 Dictionary 添加一个整数,所以我想向构造函数添加属性和一些参数。

但是,在 C# 中你不能这样做。我如何实现我想要实现的目标?

编辑:

一位更有经验的成员编辑了我的帖子,所以我将保持原样。我不关心使用字典(当时使用正确的东西)

本质上我只想要以结构方式列出三种类型的数据,除了我总是想引用该类的一次实例存储值,因此是静态的。现在我总是这样做 List of(T) ,例如:

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()
    {
    }
}

然后执行以下操作

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

但是在这种情况下,我想要类似于上面的东西,但是 static (不想创建实例等我真的很感激所有的建议,因此我编辑了我的帖子以提供更多信息。

作为注释,我可能想使用 LINQ 来提取一些记录,例如 get record where guid == guid 等。这就是我的全部内容。将用它来。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

故事还在继续 2024-09-21 01:52:29

静态构造函数是自动调用的,而不是由您调用。它无法知道要传入哪些参数。

因此,要么

  1. 将属性设置为构造函数中的值,
  2. 然后使用您添加的另一个方法设置它们,例如 Initialize(int a, ...)

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, ...)
ヤ经典坏疍 2024-09-21 01:52:29

为什么不直接使用集合初始值设定项语法?
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",
        },
    };
}

如果您需要通过 ID 查找网站,那么请务必使用字典:

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" },
    };
}

编辑之前:

请注意,字典不是列表。

但是,您当然可以随时向静态成员添加值。它不必在编译时定义:

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

如果您需要更多信息,请描述您用于“添加 int”的结构。如果您在定义该结构时遇到问题,请解释您希望字典做什么。你向它传递一个 GUID,然后得到什么信息?

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?

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