عمومی سازی الگوریتمها با استفاده از Reflection
نویسنده: علی یگانه مقدم
تاریخ: ۱۳۹۴/۰۳/۰۳ ۱۰:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<div>
<div>
<div>
<p><span>First Name </span>: Jonathan</p>
</div>
</div>
</div> @model System.Web.UI.WebControls.ListItem
<div>
<p><span>@Model.Text </span>: @Model.Value</p>
</div> @using System.Web.UI.WebControls
@using ZekrWepApp.Filters
@model ZekrModel.Admin
<div>
<h1>Bio Graph</h1>
<div>
@{
ListItemCollection collection = GetCustomProperties.Get(Model,exclude:new string[]{"Poems","Id"});
foreach (var item in collection)
{
Html.RenderPartial(MVC.Shared.Views._BioRow, item);
}
}
</div>
</div> public class GetCustomProperties
{
private static PropertyInfo[] getObjectsInfos(object obj,string[] inclue,string[] exclude )
{
var list = obj.GetType().GetProperties();
PropertyInfo[] outputPropertyInfos = null;
if (inclue != null)
{
return list.Where(propertyInfo => inclue.Contains(propertyInfo.Name)).ToArray();
}
if (exclude != null)
{
return list.Where(propertyInfo => !exclude.Contains(propertyInfo.Name)).ToArray();
}
return list;
}
} public static ListItemCollection Get(object obj,string[] inclue=null,string[] exclude=null)
{
var propertyInfos = getObjectsInfos(obj, inclue, exclude);
if (propertyInfos == null) throw new ArgumentNullException("propertyInfos is null");
var collection = new ListItemCollection();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
string name = propertyInfo.Name;
foreach (Attribute attribute in propertyInfo.GetCustomAttributes(true))
{
DisplayAttribute displayAttribute = attribute as DisplayAttribute;
if (displayAttribute != null)
{
name = displayAttribute.Name;
break;
}
}
string value = "";
object objvalue = propertyInfo.GetValue(obj);
if (objvalue != null) value = objvalue.ToString();
collection.Add(new ListItem(name,value));
}
return collection;
} [Display(Name = "نام کاربری")]
public string UserName { get; set; } using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc.Html;
using System.Web.UI.WebControls;
using Links;
namespace ZekrWepApp.Filters
{
public class GetCustomProperties
{
public static ListItemCollection Get(object obj,string[] inclue=null,string[] exclude=null)
{
var propertyInfos = getObjectsInfos(obj, inclue, exclude);
if (propertyInfos == null) throw new ArgumentNullException("propertyInfos is null");
var collection = new ListItemCollection();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
string name = propertyInfo.Name;
foreach (Attribute attribute in propertyInfo.GetCustomAttributes(true))
{
DisplayAttribute displayAttribute = attribute as DisplayAttribute;
if (displayAttribute != null)
{
name = displayAttribute.Name;
break;
}
}
string value = "";
object objvalue = propertyInfo.GetValue(obj);
if (objvalue != null) value = objvalue.ToString();
collection.Add(new ListItem(name,value));
}
return collection;
}
private static PropertyInfo[] getObjectsInfos(object obj,string[] include,string[] exclude )
{
var list = obj.GetType().GetProperties();
PropertyInfo[] outputPropertyInfos = null;
if (include != null)
{
return list.Where(propertyInfo => include.Contains(propertyInfo.Name)).ToArray();
}
if (exclude != null)
{
return list.Where(propertyInfo => !exclude.Contains(propertyInfo.Name)).ToArray();
}
return list;
}
}
} amount=1000&orderId=452&Pid=xxx&....
using System;
using System.Collections.Generic;
using System.Linq;
namespace Utils
{
public class QueryStringParametersList
{
private string Symbol = "&";
private List<KeyValuePair<string, string>> list { get; set; }
public QueryStringParametersList()
{
list = new List<KeyValuePair<string, string>>();
}
public QueryStringParametersList(string symbol)
{
Symbol = symbol;
list = new List<KeyValuePair<string, string>>();
}
public int Size
{
get { return list.Count; }
}
public void Add(string key, string value)
{
list.Add(new KeyValuePair<string, string>(key, value));
}
public string GetQueryStringPostfix()
{
return string.Join(Symbol, list.Select(p => Uri.EscapeDataString(p.Key) + "=" + Uri.EscapeDataString(p.Value)));
}
}
} QueryStringParametersList queryparamsList = new QueryStringParametersList();
ueryparamsList.Add("consumer_key", requestPayment.Consumer_Key);
queryparamsList.Add("amount", requestPayment.Amount.ToString());
queryparamsList.Add("callback", requestPayment.Callback);
queryparamsList.Add("description", requestPayment.Description);
queryparamsList.Add("email", requestPayment.Email);
queryparamsList.Add("mobile", requestPayment.Mobile);
queryparamsList.Add("name", requestPayment.Name);
queryparamsList.Add("irderid", requestPayment.OrderId.ToString()); private QueryStringParametersList ReadParams(object obj)
{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties();
QueryStringParametersList queryparamsList = new QueryStringParametersList();
for (int i = 0; i < propertyInfos.Count(); i++)
{
queryparamsList.Add(propertyInfos[i].Name.ToLower(),propertyInfos[i].GetValue(obj).ToString() );
}
return queryparamsList;
}