从 C# 中的现有静态对象向对象动态添加属性
Dynamically adding properties to an Object from an existing static object in C#
在我的 ASP .Net Web API 应用程序中进行数据库调用时,需要将一些属性添加到已经具有一些现有属性的模型 Class 中。
我知道我可以在这种情况下使用 ExpandoObject
并在 运行 时添加属性,但我想知道如何首先从现有对象继承所有属性然后添加一些.
例如,假设传递给方法的对象是 ConstituentNameInput
并且定义为
public class ConstituentNameInput
{
public string RequestType { get; set; }
public Int32 MasterID { get; set; }
public string UserName { get; set; }
public string ConstType { get; set; }
public string Notes { get; set; }
public int CaseNumber { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PrefixName { get; set; }
public string SuffixName { get; set; }
public string NickName { get; set; }
public string MaidenName { get; set; }
public string FullName { get; set; }
}
现在,在我动态创建的对象中,我想添加所有这些现有属性,然后添加一些名为 wherePartClause
和 selectPartClause
.
的属性
我该怎么做?
那么您可以创建一个新的 ExpandoObject
并使用反射使用现有对象的属性填充它:
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
var obj = new { Foo = "Fred", Bar = "Baz" };
dynamic d = CreateExpandoFromObject(obj);
d.Other = "Hello";
Console.WriteLine(d.Foo); // Copied
Console.WriteLine(d.Other); // Newly added
}
static ExpandoObject CreateExpandoFromObject(object source)
{
var result = new ExpandoObject();
IDictionary<string, object> dictionary = result;
foreach (var property in source
.GetType()
.GetProperties()
.Where(p => p.CanRead && p.GetMethod.IsPublic))
{
dictionary[property.Name] = property.GetValue(source, null);
}
return result;
}
}
在我的 ASP .Net Web API 应用程序中进行数据库调用时,需要将一些属性添加到已经具有一些现有属性的模型 Class 中。
我知道我可以在这种情况下使用 ExpandoObject
并在 运行 时添加属性,但我想知道如何首先从现有对象继承所有属性然后添加一些.
例如,假设传递给方法的对象是 ConstituentNameInput
并且定义为
public class ConstituentNameInput
{
public string RequestType { get; set; }
public Int32 MasterID { get; set; }
public string UserName { get; set; }
public string ConstType { get; set; }
public string Notes { get; set; }
public int CaseNumber { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string PrefixName { get; set; }
public string SuffixName { get; set; }
public string NickName { get; set; }
public string MaidenName { get; set; }
public string FullName { get; set; }
}
现在,在我动态创建的对象中,我想添加所有这些现有属性,然后添加一些名为 wherePartClause
和 selectPartClause
.
我该怎么做?
那么您可以创建一个新的 ExpandoObject
并使用反射使用现有对象的属性填充它:
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
var obj = new { Foo = "Fred", Bar = "Baz" };
dynamic d = CreateExpandoFromObject(obj);
d.Other = "Hello";
Console.WriteLine(d.Foo); // Copied
Console.WriteLine(d.Other); // Newly added
}
static ExpandoObject CreateExpandoFromObject(object source)
{
var result = new ExpandoObject();
IDictionary<string, object> dictionary = result;
foreach (var property in source
.GetType()
.GetProperties()
.Where(p => p.CanRead && p.GetMethod.IsPublic))
{
dictionary[property.Name] = property.GetValue(source, null);
}
return result;
}
}