C# 创建后将属性添加到匿名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/233711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add property to anonymous type after creation
提问by Boris Callens
I use an anonymous object to pass my Html Attributes to some helper methods. If the consumer didn't add an ID attribute, I want to add it in my helper method.
我使用匿名对象将我的 Html 属性传递给一些辅助方法。如果消费者没有添加 ID 属性,我想在我的辅助方法中添加它。
How can I add an attribute to this anonymous object?
如何向这个匿名对象添加属性?
采纳答案by Levitikon
If you're trying to extend this method:
如果您尝试扩展此方法:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues);
Although I'm sure Khaja's Object extensions would work, you might get better performance by creating a RouteValueDictionary and passing in the routeValues object, add your additional parameters from the Context, then return using the ActionLink overload that takes a RouteValueDictionary instead of an object:
虽然我确信 Khaja 的对象扩展会起作用,但通过创建 RouteValueDictionary 并传入 routeValues 对象,从上下文添加其他参数,然后使用 ActionLink 重载返回,该重载采用 RouteValueDictionary 而不是对象,您可能会获得更好的性能:
This should do the trick:
这应该可以解决问题:
public static MvcHtmlString MyLink(this HtmlHelper helper, string linkText, string actionName, object routeValues)
{
RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routeValues);
// Add more parameters
foreach (string parameter in helper.ViewContext.RequestContext.HttpContext.Request.QueryString.AllKeys)
{
routeValueDictionary.Add(parameter, helper.ViewContext.RequestContext.HttpContext.Request.QueryString[parameter]);
}
return helper.ActionLink(linkText, actionName, routeValueDictionary);
}
回答by Jon Skeet
I assume you mean anonymous types here, e.g. new { Name1=value1, Name2=value2}
etc. If so, you're out of luck - anonymous types are normal types in that they're fixed, compiled code. They just happen to be autogenerated.
我假设你在这里指的是匿名类型,例如new { Name1=value1, Name2=value2}
等等。如果是这样,你就不走运了 - 匿名类型是正常类型,因为它们是固定的、编译过的代码。它们恰好是自动生成的。
What you coulddo is write new { old.Name1, old.Name2, ID=myId }
but I don't know if that's really what you want. Some more details on the situation (including code samples) would be ideal.
你可以做的是写,new { old.Name1, old.Name2, ID=myId }
但我不知道这是否真的是你想要的。有关情况的更多详细信息(包括代码示例)将是理想的。
Alternatively, you could create a container object which alwayshad an ID and whatever other object contained the rest of the properties.
或者,您可以创建一个始终具有 ID的容器对象,而任何其他对象包含其余属性。
回答by Boris Callens
public static string TextBox(this HtmlHelper html, string value, string labelText, string textBoxId, object textBoxHtmlAttributes, object labelHtmlAttributes){}
This would accept the id value the textbox should have and the label should refer to. If the consumer now doesn't include the "id" property in the textBoxHtmlAttributes, the method will create an incorrect label.
这将接受文本框应具有的 id 值以及标签应引用的值。如果消费者现在不在 textBoxHtmlAttributes 中包含“id”属性,则该方法将创建不正确的标签。
I can check through reflection if this attribute is added in the labelHtmlAttributes object. If so, I want to add it or create a new anonymous object that has it added. But because I can't create a new anonymous type by walking through the old attributes and adding my own "id" attribute, I'm kind of stuck.
如果在 labelHtmlAttributes 对象中添加了此属性,我可以通过反射检查。如果是这样,我想添加它或创建一个添加它的新匿名对象。但是因为我无法通过遍历旧属性并添加我自己的“id”属性来创建新的匿名类型,所以我有点卡住了。
A container with a strongly typed ID property and then an anonymous typed "attributes" property would require code rewrites that don't weigh up to the "add an id field" requirement.
具有强类型 ID 属性和匿名类型“属性”属性的容器将需要重写不符合“添加 id 字段”要求的代码。
Hope this response is understandable. It's the end of the day, can't get my brains in line anymore..
希望这个回答是可以理解的。一天结束了,不能再让我的大脑排队了..
回答by Khaja Minhajuddin
The following extension class would get you what you need.
以下扩展类将为您提供所需的内容。
public static class ObjectExtensions
{
public static IDictionary<string, object> AddProperty(this object obj, string name, object value)
{
var dictionary = obj.ToDictionary();
dictionary.Add(name, value);
return dictionary;
}
// helper
public static IDictionary<string, object> ToDictionary(this object obj)
{
IDictionary<string, object> result = new Dictionary<string, object>();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
foreach (PropertyDescriptor property in properties){
result.Add(property.Name, property.GetValue(obj));
}
return result;
}
}