如何实现静态字典在 C# 运行时使用参数?
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
辛格尔顿。
http://en.wikipedia.org/wiki/Singleton_pattern
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
为什么不直接使用集合初始值设定项语法?
http://msdn.microsoft.com/en-us/library/bb384062。 aspx
如果您需要通过 ID 查找网站,那么请务必使用字典:
编辑之前:
请注意,字典不是列表。
但是,您当然可以随时向静态成员添加值。它不必在编译时定义:
如果您需要更多信息,请描述您用于“添加 int”的结构。如果您在定义该结构时遇到问题,请解释您希望字典做什么。你向它传递一个 GUID,然后得到什么信息?
Why not just use the collection initializer syntax?
http://msdn.microsoft.com/en-us/library/bb384062.aspx
If you need to look up a website by ID, then by all means use a dictionary:
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:
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?