내 취향이에 사용자 정의 유형을 생성하는 간단한 T4 템플릿을 사용하는 것입니다 파리. 최근 개인 프로젝트에서 사용했던 예가 있습니다. 10 행에는 생성 된 유형 목록이 있습니다. 이 모든 것들은 int
이었지만 지금은 강하게 입력됩니다.
이것은 또한 "원시적 인 강박 관념"방지 패턴을 피함으로써보다 기능적인 패러다임을 사용하도록 코드를 이동시킵니다.
내가 사용하는 템플릿이 있습니다. 언제든지 업데이트/수정하십시오 (유용한 정보를 추가하면 나머지 사용자에게 알려주십시오).
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
// List of types to generate:
var createTypeList = new[] { "XDim", "YDim", "YDelta", "DelayValue", "HValue", "Score", "TplIndexValue" };
#>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
// ReSharper disable CheckNamespace
<#
for(int i = 0; i < createTypeList.Length; i++)
{
var typeName = createTypeList[i];
#>
[ImmutableObject(true)]
public struct <#=typeName#> : IComparable<<#=typeName#>>
{
public <#=typeName#>(int value) { Value = value; }
[Pure] public int Value { get; }
[Pure] public bool Equals(<#=typeName#> other) => Value == other.Value;
[Pure]
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (Equals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((<#=typeName#>)obj);
}
[Pure]
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397)^Value;
}
}
[Pure] public static bool operator ==(<#=typeName#> left, <#=typeName#> right) => Equals(left, right);
[Pure] public static bool operator !=(<#=typeName#> left, <#=typeName#> right) => !Equals(left, right);
[Pure] public int CompareTo(<#=typeName#> other) => Equals(this, other) ? 0 : Value.CompareTo(other.Value);
[Pure] public static bool operator <(<#=typeName#> left, <#=typeName#> right) => Comparer<<#=typeName#>>.Default.Compare(left, right) < 0;
[Pure] public static bool operator >(<#=typeName#> left, <#=typeName#> right) => Comparer<<#=typeName#>>.Default.Compare(left, right) > 0;
[Pure] public static bool operator <=(<#=typeName#> left, <#=typeName#> right) => Comparer<<#=typeName#>>.Default.Compare(left, right) <= 0;
[Pure] public static bool operator >=(<#=typeName#> left, <#=typeName#> right) => Comparer<<#=typeName#>>.Default.Compare(left, right) >= 0;
[Pure] public override string ToString() => $"{nameof(Value)}: {Value}";
}
<#
}
#>
개인 값에 유형이 없습니다? 그게 맞습니까? – bendewey
@bendewey : 오타였습니다. 그것을 지적 해 주셔서 감사합니다. –
우수, 감사합니다! 에 추가 답변이 있습니다. 이후 다른 ID를 몇 가지 필요, 내가이 일을 마스터 ID 클래스를 가지고 있어야하고, 그런 다음 그냥 하위를 파생시키는 것 같습니다.그러나 이것은 내가 생각한 것처럼 작동하지 않는 것 같습니다. 는 ID에 위의 이름을 변경하고 공용 클래스 ParticipantID를 작성 : – second