reflection.emit

How to dynamically create a class?

。_饼干妹妹 提交于 2019-11-26 03:13:38
问题 I have a class which looks like this: public class Field { public string FieldName; public string FieldType; } And an object List<Field> with values: {\"EmployeeID\",\"int\"}, {\"EmployeeName\",\"String\"}, {\"Designation\",\"String\"} I want to create a class that looks like this: Class DynamicClass { int EmployeeID, String EmployeeName, String Designation } Is there any way to do this? I want this to be generated at runtime. I don\'t want a physical CS file residing in my filesystem. 回答1:

General purpose FromEvent method

戏子无情 提交于 2019-11-26 02:09:14
问题 Using the new async/await model it\'s fairly straightforward to generate a Task that is completed when an event fires; you just need to follow this pattern: public class MyClass { public event Action OnCompletion; } public static Task FromEvent(MyClass obj) { TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); obj.OnCompletion += () => { tcs.SetResult(null); }; return tcs.Task; } This then allows: await FromEvent(new MyClass()); The problem is that you need to create a new