8

有没有办法向匿名类添加额外的属性(或使用额外的属性创建一个新实例)?

string cssClasses = "hide";
object viewData = new { @class = cssClasses };
if (suppliedId != null)
{
  // add an extra id property to the viewData somehow...
  // desired end result is { @id = suppliedId, @class = cssClasses }
}
// ... code will be passing the viewData as the additionalViewData parameter in asp.mvc's .EditorFor method
4

4 回答 4

6

不,你不能这样做。不过,您可以重新定义它:

dynamic viewData = new { @class = cssClasses };
if (suppliedId != null)
{
    viewData = new { @class = viewData.@class, yourExtra = Property };
}

请注意,我将其更改为dynamic. 这只是为了您可以编辑属性而无需转换为dynamic.

于 2013-07-23T07:50:30.123 回答
3

您不能向匿名类型添加额外的属性,但可以使用两种匿名类型。

如果你不喜欢dynamic(并且有理由不喜欢),你可以这样做:

object viewData;
if (suppliedId == null)
    viewData = new { @class = cssClasses, };
else
    viewData = new { @id = suppliedId, @class = cssClasses, };

如果要保留对每个对象实例的单独精确类型的引用,请使用:

var viewDataSimple = new { @class = cssClasses, };
var viewDataExtra = new { @id = suppliedId, @class = cssClasses, };
object viewDataToUse;
if (suppliedId == null)
    viewDataToUse = viewDataSimple;
else
    viewDataToUse = viewDataExtra;

反而。

于 2013-07-23T08:25:04.173 回答
3

感谢 Patryk 的评论,我尝试使用 ExpandoObject 并让它像这样工作:

dynamic viewData = new ExpandoObject();
viewData.@class = cssClasses;
if (controlId != null)
  viewData.id = controlId;
if (title != null)
  viewData.title = title;
// put the result into a route value dictionary so that MVC's EditorFor (etc) can interpret it
var additionalViewData = new RouteValueDictionary(viewData);

最后一行是让它在 MVC 中工作的关键,以便它可以作为 EditorFor 等中的 AdditionalViewData 参数传递。

在我被传递一个匿名类并且需要自己添加到它的情况下,我正在使用反射(并利用 ExpandoObject 实现 IDictionary)。这是我编写的用于检查它是否有效的单元测试:

[TestMethod]
public void ShouldBeAbleToConvertAnAnonymousObjectToAnExpandoObject()
{
  var additionalViewData = new {id = "myControlId", css = "hide well"};
  dynamic result = new ExpandoObject();
  var dict = (IDictionary<string, object>)result;
  foreach (PropertyInfo propertyInfo in additionalViewData.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
  {
    dict[propertyInfo.Name] = propertyInfo.GetValue(additionalViewData, null);
  }
  Assert.AreEqual(result.id, "myControlId");
  Assert.AreEqual(result.css, "hide well");
}
于 2013-07-24T01:47:36.300 回答
0

您可以通过使用Reflection来实现要求,您可以通过 KeyValue Pair 添加新属性。

string cssClasses = "hide";
        object viewData = new { @class = cssClasses };
        dynamic expandoData = new ExpandoObject();
        var processedData = expandoData as IDictionary<String, object>;
        if (viewData != null)
        {
            PropertyInfo[] properties = viewData.GetType().GetProperties();
            foreach (var prop in properties)
            {
                if (prop == null) continue;
                object valueObj = viewData .GetType().GetProperty(prop.Name).GetValue(viewData , null);
                processedData[prop.Name.ToString()] = valueObj;
            }
        }           
        processedData["AdditionalPropertyName"] = AdditionalProperty;
        var infoJson = JsonConvert.SerializeObject(processedData);
于 2016-06-01T12:36:13.260 回答