目前,我正在手動向對象添加屬性和值,如示例,併發送到Dapper.SimpleCRUD以從Dapper Orm中獲取數據。這是我想達到的想要的輸出。使用C#動態構建強類型對象?
object whereCriteria = null;
whereCriteria = new
{
CountryId = 2,
CountryName = "Anywhere on Earth",
CountryCode = "AOE",
IsActive = true
};
下面的類應該以上述格式構建對象並返回現成的對象。
public static class WhereClauseBuilder
{
public static object BuildWhereClause(object model)
{
object whereObject = null;
var properties = GetProperties(model);
foreach (var property in properties)
{
var value = GetValue(property, model);
//Want to whereObject according to the property and value. Need help in this part!!!
}
return whereObject;
}
private static object GetValue(PropertyInfo property, object model)
{
return property.GetValue(model);
}
private static IEnumerable<PropertyInfo> GetProperties(object model)
{
return model.GetType().GetProperties();
}
}
此功能WhereClauseBuilder.BuildWhereClause(object model)
應預期格式(mentiond上文)返回對象。這是我想如何使用的實現。
public sealed class CountryModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryCode { get; set; }
public bool IsActive { get; set; }
}
public class WhereClauseClass
{
public WhereClauseClass()
{
var model = new CountryModel()
{
CountryCode = "AOE",
CountryId = 2,
CountryName = "Anywhere on Earth",
IsActive = true
};
//Currently, won't return the correct object because the implementation is missing.
var whereClauseObject = WhereClauseBuilder.BuildWhereClause(model);
}
}
此鏈接[http://msdn.microsoft.com/en-us/library/system.activator.createinstance(VS.71).aspx](http://msdn.microsoft.com/en-us/ library/system.activator.createinstance(VS.71).aspx)會幫助你。 – Thennarasan
謝謝,我已經看到了這個鏈接。但是,我很好奇知道如何構建可以以所需格式返回的對象? –
我不明白你想要做什麼。您可以創建一個[ExpandoObject](https://msdn.microsoft.com/de-de/library/system.dynamic.expandoobject(v = vs.110).aspx)的實例並將其與動態配合使用,我不確定這是你想要的。另一種方法是創建一個代碼模板,其中包含所有需要的名稱空間和類體,並填寫屬性定義。有了這個,你可以在運行時編譯代碼並創建對象的一個實例。在這種情況下,Microsoft.CSharp將幫助你。 –