عنوان:

‫مروری بر کاربردهای Anonymous Types


نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۴/۰۳ ۰:۵۶
آدرس: www.dntips.ir


مشاهده مطلب اصلی

نظرات

  • مهمان در ۱۳۹۲/۰۲/۱۵ ۱۱:۵۶
    سلام
    مواقعی که یه تابع یه anonymous type رو به عنوان ورودی دریافت میکنه از کجا میفهمه من چه پروپرتی هایی رو براش ست کردم؟ 
    مثلا: html.actionlink تو mvc یه پیاده سازی داره که html attribute رو به صورت anonymous type میگیره ولی من درک نکردم داخلش چطوری میفهمه من چیا رو فرستادم؟
    • وحید نصیری در ۱۳۹۲/۰۲/۱۵ ۱۲:۵۶
      سورس MVC در دسترس هست:
              public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) {
                  RouteValueDictionary result = new RouteValueDictionary();
      
                  if (htmlAttributes != null) {
                      foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes)) {
                          result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
                      }
                  }
      
                  return result;
              }
      از متد فوق و Reflection برای استخراج خواص تعریف شده استفاده می‌کنه.