如何将动态成员名称和值添加到 ExpandoObject class 的实例?
How to add dynamic member name and value to an instance of ExpandoObject class?
假设成员名称列表存储在如下数组中,
string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
我编写了以下代码来生成动态 class 和成员以及随机值。
var myDynamicClassList = new List<ExpandoObject>();
foreach (var MemberName in myMembers)
{
dynamic dynamicClass = new ExpandoObject();
dynamicClass.MemberName = new Random().Next();
myDynamicClassList.Add(dynamicClass);
}
为了显示上面的输出myDynamicClassList
我写了下面的代码
foreach (var x in myDynamicClassList)
{
foreach (var property in (IDictionary<String, Object>)x)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
}
正在显示这样的输出
MemberName : 123465461
MemberName : 564613611
MemberName : 134654321
MemberName : 786451214
但是我期望的输出不是上面的输出
ChapterName : 123465461
Medium : 564613611
LastName : 134654321
OrderID : 786451214
这是我的问题,是否可以在 c# 中将动态成员名称添加到动态 class。如果可以请告诉我,如果不能请指导我完成这项工作。
非常感谢您的帮助。
如 similar question ("Dynamically adding properties to an ExpandoObject") 中所述,您可以直接使用 IDictionary
来完成此操作:
string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
var myDynamicClassList = new List<ExpandoObject>();
Random random = new Random();
foreach (var MemberName in myMembers)
{
IDictionary<string, object> dynamicClass = (IDictionary<string, object>)(new ExpandoObject());
dynamicClass.Add(MemberName, random.Next());
myDynamicClassList.Add((ExpandoObject)dynamicClass);
}
foreach (var x in myDynamicClassList)
{
foreach (var property in (IDictionary<String, Object>)x)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
}
但是,您应该了解此方法可能存在的已知内存限制,如上面链接的 post 或 Microsoft issue 的评论中所述。我可能会重新考虑设计以考虑实际需要哪些属性,或者在可行的情况下简单地使用 Dictionary
(完全避免动态 ExpandoObject
)。
我还把你的 Random
移到了循环之外。 Creating new instances of Random
inside a loop may not give you the results you expect...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
namespace Deligate
{
class Program
{
public static List<Employee> empList = new List<Employee>();
public static void UserInfo(int val)
{
Employee emp = new Employee();
Console.WriteLine($"Enter {val+1} Employee Details :- ");
Console.WriteLine("Enter Your Name: ");
emp.Name= Console.ReadLine();
Console.WriteLine("Enter your Email: ");
emp.Email = Console.ReadLine();
Console.WriteLine("Enter Mobile Number");
emp.Mobile = Console.ReadLine();
empList.Add(emp);
}
public static void CountEmp()
{
Console.WriteLine("How many user you want to add:");
var UserCount = int.Parse(Console.ReadLine());
for (int i = 0; i < UserCount; i++)
{
UserInfo(i);
}
foreach (var employee in empList) {
Console.WriteLine(employee.Name+ ", " + employee.Email+", "+employee.Mobile);
}
System.IO.File.Delete(@"D:\Compare.txt");
foreach (var el in empList)
{
System.IO.File.AppendAllText(@"D:\Compare.txt", el.Name+", "+el.Email+", "+el.Mobile + Environment.NewLine);
}
}
public static void Main(string[] args)
{
CountEmp();
Console.ReadKey();
}
}
}
假设成员名称列表存储在如下数组中,
string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
我编写了以下代码来生成动态 class 和成员以及随机值。
var myDynamicClassList = new List<ExpandoObject>();
foreach (var MemberName in myMembers)
{
dynamic dynamicClass = new ExpandoObject();
dynamicClass.MemberName = new Random().Next();
myDynamicClassList.Add(dynamicClass);
}
为了显示上面的输出myDynamicClassList
我写了下面的代码
foreach (var x in myDynamicClassList)
{
foreach (var property in (IDictionary<String, Object>)x)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
}
正在显示这样的输出
MemberName : 123465461
MemberName : 564613611
MemberName : 134654321
MemberName : 786451214
但是我期望的输出不是上面的输出
ChapterName : 123465461
Medium : 564613611
LastName : 134654321
OrderID : 786451214
这是我的问题,是否可以在 c# 中将动态成员名称添加到动态 class。如果可以请告诉我,如果不能请指导我完成这项工作。
非常感谢您的帮助。
如 similar question ("Dynamically adding properties to an ExpandoObject") 中所述,您可以直接使用 IDictionary
来完成此操作:
string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
var myDynamicClassList = new List<ExpandoObject>();
Random random = new Random();
foreach (var MemberName in myMembers)
{
IDictionary<string, object> dynamicClass = (IDictionary<string, object>)(new ExpandoObject());
dynamicClass.Add(MemberName, random.Next());
myDynamicClassList.Add((ExpandoObject)dynamicClass);
}
foreach (var x in myDynamicClassList)
{
foreach (var property in (IDictionary<String, Object>)x)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
}
但是,您应该了解此方法可能存在的已知内存限制,如上面链接的 post 或 Microsoft issue 的评论中所述。我可能会重新考虑设计以考虑实际需要哪些属性,或者在可行的情况下简单地使用 Dictionary
(完全避免动态 ExpandoObject
)。
我还把你的 Random
移到了循环之外。 Creating new instances of Random
inside a loop may not give you the results you expect...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
namespace Deligate
{
class Program
{
public static List<Employee> empList = new List<Employee>();
public static void UserInfo(int val)
{
Employee emp = new Employee();
Console.WriteLine($"Enter {val+1} Employee Details :- ");
Console.WriteLine("Enter Your Name: ");
emp.Name= Console.ReadLine();
Console.WriteLine("Enter your Email: ");
emp.Email = Console.ReadLine();
Console.WriteLine("Enter Mobile Number");
emp.Mobile = Console.ReadLine();
empList.Add(emp);
}
public static void CountEmp()
{
Console.WriteLine("How many user you want to add:");
var UserCount = int.Parse(Console.ReadLine());
for (int i = 0; i < UserCount; i++)
{
UserInfo(i);
}
foreach (var employee in empList) {
Console.WriteLine(employee.Name+ ", " + employee.Email+", "+employee.Mobile);
}
System.IO.File.Delete(@"D:\Compare.txt");
foreach (var el in empList)
{
System.IO.File.AppendAllText(@"D:\Compare.txt", el.Name+", "+el.Email+", "+el.Mobile + Environment.NewLine);
}
}
public static void Main(string[] args)
{
CountEmp();
Console.ReadKey();
}
}
}