您创建一个具有字符串名称的对象并设置其属性:
public class Test
{
//All my classes have these properties
//You can set up an interface and in the method you can set entity to an interface type
//You can even put these interfaces on edmx generated entities
//http://stackoverflow.com/questions/14059455/adding-validation-attributes-with-an-entity-framework-data-model
public string AString { get; set; }
public DateTime ADate { get; set; }
}
public class HomeController : Controller
{
public ActionResult IndexStackOverflow101()
{
Assembly assembly = Assembly.Load("Testy20161006");
Type t = assembly.GetType("Testy20161006.Controllers." + "Test");
Object entity = (Object)Activator.CreateInstance(t);
PropertyInfo entityProperty = t.GetProperty("AString");
PropertyInfo entityPropertyTwo = t.GetProperty("ADate");
entityProperty.SetValue(entity, Convert.ChangeType("ap", entityProperty.PropertyType), null);
entityPropertyTwo.SetValue(entity, Convert.ChangeType(DateTime.Now, entityPropertyTwo.PropertyType), null);