比较IEnumerable 的类型(Compare type of IEnumerable)
我有字典,根据传入的值返回一个方法。定义如下:
Dictionary<Type, IXmlWriterConverter>
我现在添加了一个新的函数,其Key / type设置为IEnumerable,到目前为止一直很好。 但是当我使用包含两个DataTables的List执行单元测试但字典找不到密钥时,例如我的类型转换不同。
为什么? 什么是解决我的问题的正确尝试?
编辑:对不起,这是请求的代码;-)
生成测试值的函数:
public IEnumerable<DataTable> CreateTestDataTableList() { var resultDataTable = new List<DataTable>(); resultDataTable.Add(CreateTestTable("testTable1", 2)); resultDataTable.Add(CreateTestTable("testTable2", 3)); return resultDataTable; }
单元测试调用的函数:
public void Write(XmlWriter xmlWriter, object value) { ... converter = FindConverter(value.GetType()); }
检查字典的功能:
public IXmlWriterConverter FindConverter(Type type) { if(Converters.ContainsKey(type)) { return Converters[type]; } return null; }
2.Edit:
将值添加到Dictionary的代码:
public void Add(IXmlWriterConverter xmlWriterConverter) { if(Converters.ContainsKey(xmlWriterConverter.InputType)) { Remove(xmlWriterConverter); } Converters.Add(xmlWriterConverter.InputType, xmlWriterConverter); }
InputType是转换器的只读(get)属性。 我检查了添加到字典中的类型,并注册为IEnumerable,但是当我在列表中传递时检查typeof时,类型是List而不是IEnumerable。 我被告知发生这种情况是因为我将值作为对象传递。
I have dictionary that gives me back a method according to the value passed in. Defined as so:
Dictionary<Type, IXmlWriterConverter>
I have now added a new function that which has the Key/type set to IEnumerable, so far so good. But when I execute my unit test with a List containing two DataTables but the dictionary can not find the key e.g. my type conversion differs.
Why is that so? And what would be the right attempt to solve my problem?
Edit: Sorry here is the requested code ;-)
Function that generates the testvalues:
public IEnumerable<DataTable> CreateTestDataTableList() { var resultDataTable = new List<DataTable>(); resultDataTable.Add(CreateTestTable("testTable1", 2)); resultDataTable.Add(CreateTestTable("testTable2", 3)); return resultDataTable; }
Function called by the unit test:
public void Write(XmlWriter xmlWriter, object value) { ... converter = FindConverter(value.GetType()); }
Function checking the dictionary:
public IXmlWriterConverter FindConverter(Type type) { if(Converters.ContainsKey(type)) { return Converters[type]; } return null; }
2.Edit:
Code that adds the values to the Dictionary:
public void Add(IXmlWriterConverter xmlWriterConverter) { if(Converters.ContainsKey(xmlWriterConverter.InputType)) { Remove(xmlWriterConverter); } Converters.Add(xmlWriterConverter.InputType, xmlWriterConverter); }
The InputType is a readonly (get) property of the converter. I checked the type added to the dictionary and that was registered as IEnumerable, however when I checked on the typeof when passing in my list the type was List and not IEnumerable. I was told that this happens because I pass in the values as object.
原文:https://stackoverflow.com/questions/3708917
最满意答案
你可以循环遍历所有
img
标签,并使用wrapAll()
方法。var imgs = $('body > img'); for (var i = 0; i < imgs.length; i += 2) { var add = imgs.eq(i).add(imgs.eq(i + 1)); add.wrapAll('<div class="row"><div class="col-md-6"></div></div>'); }
.col-md-6 { background-color: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <p>test</p> <img src="test.png" /> <img src="test2.png" /> <p>test</p> <img src="test3.png" /> <img src="test4.png" /> <p>test</p> </body>
You can loop through all
img
tags in body and usewrapAll()
method like following.var imgs = $('body > img'); for (var i = 0; i < imgs.length; i += 2) { var add = imgs.eq(i).add(imgs.eq(i + 1)); add.wrapAll('<div class="row"><div class="col-md-6"></div></div>'); }
.col-md-6 { background-color: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <p>test</p> <img src="test.png" /> <img src="test2.png" /> <p>test</p> <img src="test3.png" /> <img src="test4.png" /> <p>test</p> </body>
相关问答
更多-
以zend形式添加img标签(adding img tag in zend form)[2023-11-05]
在library / Application / Form / Element / Img.php中有 class Application_Form_Element_Img extends Zend_Form_Element_Xhtml { public $helper = 'formImg'; public function loadDefaultDecorators () { parent::loadDefaultDecorators (); $ ... -
使用after('Some text')而不是append('Some text') 。 append()方法将内容放在指定元素内,而after()将内容放在指定元素之后。 Use after('Some text') instead of append('Some text'). append() method puts the content inside the specified element whereas after() puts the content after the specified ...
-
请尝试以下简单易用的解决方案: var img_src_1 = "https://example.com/image-3.jpg"; var img_src_2 = "https://example.com/image-5.jpg"; jQuery("img").each(function() { if(jQuery(this).attr("src") == img_src_1 || jQuery(this).attr("src") == im ...
-
echo一个标签(echo an tag)[2022-03-29]
不确定我是否完全理解,但试试这个: echo ''; Not sure if I completely understand, but try this: echo ''; -
-
我无法评论或投票,但尤内斯的回答是错误的。 你不能像这样写一个图像标签: 此外,寻找允许和不允许使用HTML标签的最佳位置,最好的来源是实际规格,可在W3C网站上找到。 以下是HTML 4.01的img标签的链接: http://www.w3.org/TR/html4/struct/objects.html#h-13.2 这里的链接显示了HTML 4.01和XHTML 1.0之间的区别(它来自哪里: http://www.w3.org/TR/xhtml1/#diff ...
-
文章标签和img(The article tag and the img)[2022-06-19]
您可能想要检查figure和figcaption标签。 来自文档的示例: 如果这个figure与article ,那么我会将它放在article标签中。 You may want to ... -
如何在第一个img标签和第二个img标签之前添加html代码(how can I add html code before the 1st img tag and after 2 img tag)[2023-09-18]
你可以循环遍历所有img标签,并使用wrapAll()方法。 var imgs = $('body > img'); for (var i = 0; i < imgs.length; i += 2) { var add = imgs.eq(i).add(imgs.eq(i + 1)); add.wrapAll(''); } .col-md-6 { background- ... -
To add caption to the images being displayed in lightbox.Follow the below steps: - Go to admin dashboard and click the library,it will list out all uploaded pictures used for lightbox.Then click on the image for which you want to give caption Then,t ...
-
不幸的是,Pandoc <1.16 不支持其内部AST中的图像大小 。 所以目前这是不可能的。 但是,一旦调整了Docx Reader,这将有效。 Unfortunately, Pandoc <1.16 doesn't support image sizes in its internal AST. So this is currently not possible. But once the Docx Reader is adjusted as well, this will work.