Note that there are some explanatory texts on larger screens.

plurals
  1. POCan the DisplayName attribute be accessed without using MVC?
    primarykey
    data
    text
    <p>I have refactored out a generic CSV builder for List using lambda expressions to access the correct values</p> <pre><code>public static string ToCsv&lt;TModel&gt;(this List&lt;TModel&gt; list, string delimiter, string lineBreak, string valueWrap, params Expression&lt;Func&lt;TModel, object&gt;&gt;[] expressions) { var sb = new StringBuilder(); var headers = expressions.Select(m =&gt; String.Format("{0}{1}{0}", valueWrap, GetPropertyName(m))).ToArray(); sb.Append(String.Format("{0}{1}", String.Join(delimiter, headers), lineBreak)); foreach (var listItem in list) { var values = expressions.Select(m =&gt; String.Format("{0}{1}{0}", valueWrap, m.Compile()(listItem))).ToArray(); sb.Append(String.Format("{0}{1}", String.Join(delimiter, values), lineBreak)); } return sb.ToString(); } </code></pre> <p>This works well, however because I am trying to move this into some common code for several projects across several servers to access. I cannot reference the <code>System.Web.Mvc</code> assembly</p> <p>Is there a good way to access the <code>DisplayName</code> Attribute and if that doesnot exist, access the variable name?</p> <p>My current attempt access the <code>((MemberExpression) expression.Body).Member.Name</code> however it will not work if it has to convert a value to string. (ie passing an int and implicitly converting)</p> <p>A second attempt of iterating over the attributes did not work for inner class (ie <code>model</code> => <code>model.innerClass.property</code>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload