构造函数使用字典 [英] Constructor using a dictionary

查看:86
本文介绍了构造函数使用字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我需要编写通用函数的帮助.

public class MyClass
{
   public type1 obj1;
   public type2 obj2;

public MyClass (Dictionary<String, String> dico)
{
   // TODO...
   //Dico contains <"type1", "value1"> and <"type2", "value2) 
   //I want to assign automatically value1 in this.obj1 et value2 in this.obj2
}



我班上有很多东西.为了构建字典,我解析了一个像"type1 = value1"这样的字符串.

想您的帮助

解决方案

您甚至都没有尝试编写任何通用的东西.您在哪里使用<>"在语法上,您所做的恰恰相反—实例化泛型类.类System.Collections.Generic.Dictionary是通用的,类System.Collections.Generic.Dictionary<string,>不是通用的.

您确实需要学习语法和泛型的概念.

在您的情况下,您需要以下内容:

  class  MyClass< KEY,VALUE> {// 顺便说一句,考虑一下您是否真的需要它公开
   内部 MyClass(Dictionary< KEY,VALUE>字典){
      .Dictionary=字典;
   }
   字典< KEY,VALUE>字典;
   //  ... 
} //  class MyClass 

// 用法:
字典< KEY,VALUE>字典= 字典< KEY,VALUE>();
MyClass< string,string>实例=  MyClass< string,string>(字典); 



更好的是,考虑在类内部实例化字典.有什么理由要使用外部字典吗?
另外,请记住您的字典属于引用类型(一个类).如果将同一个字典传递给MyClass的两个不同实例,则在一个实例中添加一个元素将更改另一个实例中的字典,因为它们引用相同的Dictionary对象.您真的需要吗?

您可以在声明它的地方构造它.然后,您将不需要具有此参数的构造函数.

因此,如果您改变主意,它将是:

  class  MyClass< KEY,VALUE> {// 顺便说一句,考虑一下您是否真的需要它公开
   字典< KEY,VALUE>字典= 字典< KEY,VALUE>();
   // 使用字典的一些代码
   //  ... 
} //  class MyClass 

// 用法:
MyClass< string,string>实例=  MyClass< string,string>(); 



—SA


首先,这与通用函数无关:它们的形式为

 公共  void  MyFunction< T>(T myParameter){} 

不是您的代码.

我想要做的是将字典中的值分配给一个类级别的变量,其类型作为字典的键给出.

您可以使用Reflection来做到这一点,但这非常讨厌并且容易出错:如果类中有两个type1对象,该怎么办?还是没有?

也许,如果您解释您试图做的事情,您认为这是一个很好的解决方案,那么可能会有更好的解决方案.

顺便说一句:将字段显示为public是个坏主意-您应该改用属性(如有必要,可以使用自动属性).


hi. I need help to write a generic function.

public class MyClass
{
   public type1 obj1;
   public type2 obj2;

public MyClass (Dictionary<String, String> dico)
{
   // TODO...
   //Dico contains <"type1", "value1"> and <"type2", "value2) 
   //I want to assign automatically value1 in this.obj1 et value2 in this.obj2
}



I have many objects in my class. To build the dictionary I parsed a String like this "type1 = value1".

Think you for your help

解决方案

You are not even trying to write anything generic. Where you use "<>" syntax you do something just the opposite — instantiate generic class. The class System.Collections.Generic.Dictionary is generic, the class System.Collections.Generic.Dictionary<string,> is not.

You really need to learn syntax and the idea of generics.

In your case, you need something like:

class MyClass<KEY, VALUE> { //by the way, think if you really need it public
   internal MyClass(Dictionary<KEY, VALUE> dictionary) {
      this.Dictionary = dictionary;
   }
   Dictionary<KEY, VALUE> Dictionary;
   //...
} //class MyClass

//usage:
Dictionary<KEY, VALUE> dictionary = new Dictionary<KEY, VALUE>();
MyClass<string, string> instance = new MyClass<string, string>(dictionary); 



Better yet, think about instantiating of the dictionary inside class. Is there any reason to use external dictionary?
Also, remember that your dictionary is of the reference type (a class). If you pass the same dictionary to two different instances of MyClass, adding an element in one instance will change the dictionary in another one because they reference the same Dictionary object. Do you really need that?

You can construct it where it is declared. You won''t need a constructor with this parameter then.

So, if you change your mind, it will be:

class MyClass<KEY, VALUE> { //by the way, think if you really need it public
   Dictionary<KEY, VALUE> Dictionary = new Dictionary<KEY, VALUE>();
   //some code using Dictionary
   //...
} //class MyClass

//usage:
MyClass<string, string> instance = new MyClass<string, string>(); 



—SA


First off, this has nothing to do with generic functions: they are of the form

public void MyFunction<T>(T myParameter) {}

Which your code isn''t.

What you want to do is, I think, assign the value from a dictionary to a class level variable whose type is given as the dictionary key.

You can do that, with Reflection, but it a extremely nasty and prone to error: what do you do if there are two objects of type1 in the class? Or none?

Perhaps if you explain what you are trying to do that you think this is a good solution to, there may be a better solution.

BTW: Exposing fields as public is a bad idea - you should use a property (automatic if necessary) instead.


这篇关于构造函数使用字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆