csharp 创建dataannotation dynamic.cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 创建dataannotation dynamic.cs相关的知识,希望对你有一定的参考价值。

[Test]
public void SimplestLabel()
{
   var className = "SimplestLabelClass";
   var dataAnnotation = "[DisplayName(\"Your Username: \")]";

   var formattedClass = string.Format(unformattedClass,
     className,
     dataAnnotation);

   var model = CreateDynamicModel(formattedClass, className);

   var html = HtmlHelperFactory.Create(model);
}

private object CreateDynamicModel(string formattedClass, string className)
{    
  object result;
  using (var csharp = new Microsoft.CSharp.CSharpCodeProvider())
  {
    var res = csharp.CompileAssemblyFromSource(
        new System.CodeDom.Compiler.CompilerParameters() 
        {  
            GenerateInMemory = true 
        }, 
        formattedClass
    );

    var classType = res.CompiledAssembly.GetType(className);

    result = Activator.CreateInstance(type);
  }

  return result;
}    
public static readonly unformattedClass = "public class {0} {{ {1}public string MyString {{ get; set; }} }

以上是关于csharp 创建dataannotation dynamic.cs的主要内容,如果未能解决你的问题,请参考以下文章

Asp.net DataAnnotations Regex 根据之前的 Char 评估 Char

EntityFramework Code-First 简易教程-------领域类配置之DataAnnotations

DataAnnotations - InverseProperty Attribute:

使用 DataAnnotations 比较两个模型属性

DataAnnotations [Phone] 属性

ASP.NET-表单验证-DataAnnotations