带有POCO DTO IValidatableObject的TypeLite

时间:2015-11-24 13:26:51

标签: .net typelite

我的DTO课程有IValidatableObject,当我尝试将它们与TypeLite一起使用时,我收到错误:

'System.ComponentModel.DataAnnotations.IValidatableObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

如何解决此问题?

这是我的tt文件,TypeLite.Net4.tt:

<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="$(TargetDir)TypeLite.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
<#@ assembly name="$(TargetDir)$(TargetFileName)" #>
<#@ import namespace="TypeLite" #> 
<#@ import namespace="TypeLite.Net4" #> 
<#@output extension=".d.ts"#>
<#@include file="Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>
<# 
var ts = TypeScript.Definitions()
        .WithReference("Enums.ts")
        .WithMemberFormatter((identifier) => Char.ToLower(identifier.Name[0]) + identifier.Name.Substring(1))
        .For<Models.ProjectDTO>()
        ;
#>
<#= ts.Generate(TsGeneratorOutput.Properties) #>
<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>

以下是其中一个模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Models
{
    public class ProjectDTO : IValidatableObject
    {
        public Guid ProjectId { get; set; }

        [Required]
        public Guid ClientId { get; set; }
        public ClientDTO Client { get; set; }

        [Required, MaxLength(10)]
        public string Code { get; set; }

        //snipped for brevity

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (EndDate.HasValue && EndDate < StartDate)
                yield return new ValidationResult("Project end date cannot be before the start date", new[] { "EndDate" });
        }
    }

}

1 个答案:

答案 0 :(得分:1)

将此行添加到TypeLite.Net4.tt文件的顶部应该很简单:

<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="$(TargetDir)TypeLite.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>   
<#@ assembly name="System.ComponentModel.DataAnnotations.dll" #>
...

即:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:)name:UIKeyboardWillShowNotification object:nil]