2012-10-04 3 views
2

ExpandoObject을 익명 형식 개체로 변환 할 수 있습니까?ExpandoObject를 익명 형식으로 변환

현재 확장명은 HtmlHelper이며 HTML 속성을 매개 변수로 사용할 수 있습니다. 문제는 내 확장 기능이 HTML 속성을 추가해야하므로 사용자가 htmlAttributes 매개 변수를 사용하여 함수에 전달하는 속성 및 특성을 병합하기 위해 ExpandoObject를 사용했기 때문입니다. 이제 원본 HtmlHelper 함수에 병합 된 HTML 특성을 전달해야하며 ExpandoObject를 보낼 때 아무 일도 발생하지 않습니다. 그래서 ExpandoObject를 익명으로 형식화 된 객체 또는 비슷한 것으로 변환해야한다고 생각합니다. 어떤 제안이라도 환영합니다.

+1

도우미 코드를 표시 할 수 있습니까? 그것은 당신의 목표를 잘 설명 할 것입니다. 나는 당신이 어떤 ExpandoObject도 필요로하지 않으며 당신의 목표를 성취 할 다른 방법이있을 것이라고 생각합니다. –

+0

@DarinDimitrov : 예, 맞습니다 :) Dictionary 의 문제를 해결했습니다. [ExpandoObject를 익명 형식으로 캐스트] 가능한 – xx77aBs

+0

복제본 (http://stackoverflow.com/questions/10241776/cast- expandoobject-to-anonymous-type) – nawfal

답변

3

나는 당신이 당신의 목표를 달성하기 위해 expandos 처리 할 필요가 있다고 생각하지 않습니다

public static class HtmlExtensions 
{ 
    public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes) 
    { 
     var builder = new TagBuilder("div"); 

     // define the custom attributes. Of course this dictionary 
     // could be dynamically built at runtime instead of statically 
     // initialized as in my example: 
     builder.MergeAttribute("data-myattribute1", "value1"); 
     builder.MergeAttribute("data-myattribute2", "value2"); 

     // now merge them with the user attributes 
     // (pass "true" if you want to overwrite existing attributes): 
     builder.MergeAttributes(new RouteValueDictionary(htmlAttributes), false); 

     builder.SetInnerText("hello world"); 

     return new HtmlString(builder.ToString()); 
    } 
} 

및 기존 헬퍼의 일부를 호출하기를 원한다면, 다음 간단한 foreach 루프가 일을 할 수 있습니다 :

public static class HtmlExtensions 
{ 
    public static IHtmlString MyHelper(this HtmlHelper htmlHelper, object htmlAttributes) 
    { 
     // define the custom attributes. Of course this dictionary 
     // could be dynamically built at runtime instead of statically 
     // initialized as in my example: 
     var myAttributes = new Dictionary<string, object> 
     { 
      { "data-myattribute1", "value1" }, 
      { "data-myattribute2", "value2" } 
     }; 

     var attributes = new RouteValueDictionary(htmlAttributes); 
     // now merge them with the user attributes 
     foreach (var item in attributes) 
     { 
      // remove this test if you want to overwrite existing keys 
      if (!myAttributes.ContainsKey(item.Key)) 
      { 
       myAttributes[item.Key] = item.Value; 
      } 
     } 
     return htmlHelper.ActionLink("click me", "someaction", null, myAttributes); 
    } 
} 
+0

고마워! Dictionary 로 htmlAttributes를 제공 할 수 있다는 것을 잊었습니다. – xx77aBs

+1

예, RouteValueDictionary 또는 IDictionary 를 사용하는 사용자 정의 도우미의 오버로드를 항상 제공하는 것이 좋습니다. 익명 객체를 사용하는 것 외에도 라우트 값 및 html 속성과 같은 것들을 위해. –

3

익명으로 입력 된 개체로 ExpandoObject를 변환 할 수 있습니까?

실행 시간에 익명 형식을 생성하는 경우에만.

익명 형식은 일반적으로 컴파일 타임에 컴파일 타임에 만들어지며 다른 형식과 마찬가지로 어셈블리에 구워집니다. 그들은 어떤 의미에서든 역동적이지 않습니다. 그래서 CodeDOM 같은 것을 사용하여 익명의 타입에 사용되는 것과 같은 종류의 코드를 생성해야합니다. 이것은 재미 있지 않을 것입니다.

다른 사람이 ExpandoObject에 대해 알고 있거나 (또는 ​​IDictionary<string, object>으로 만 작업 할 수있는) MVC 도우미 클래스를 만든 가능성이 높다고 생각합니다.