첫 번째로,이 기능의 사용은 학습 용이며 기능 응용 프로그램에서는 사용되지 않을 것입니다.C# 사용자 지정 날짜 클래스, 문자열 형식
는 I는 Date
객체 (사용자 정의)를 수신 (DateHandler
이름) 클래스를 생성하고, 그것이 일치의에 각 문자를 변경하는 문자열를 수신하는 기능을 가지고있다. 예를 들어
:
"d/m/Y"
미리 정의되지 않는 문자가 변경되지 않습니다 "1/1/2005"
d => Day
m => Month
Y => Full Year
주를 반환 할 수 있습니다.
Date
클래스 :
class Date
{
#region Properties
private int _Day;
private int _Month;
public int Day
{
get
{
return _Day;
}
set
{
if (value < 1)
throw new Exception("Cannot set property Date.Day below 1");
this._Day = value;
}
}
public int Month
{
get
{
return _Month;
}
set
{
if (value < 1)
throw new Exception("Cannot set property Date.Month below 1");
this._Month = value;
}
}
public int Year;
#endregion
#region Ctors
public Date() { }
public Date(int Day, int Month, int Year)
{
this.Day = Day;
this.Month = Month;
this.Year = Year;
}
#endregion
}
DateHandler
클래스 :
class DateHandler
{
#region Properties
private Date Date;
private Dictionary<char, string> Properties;
private int Properties_Count;
#endregion
#region Ctors
public DateHandler()
{
Properties = new Dictionary<char, string>();
}
public DateHandler(Date Date)
: this()
{
this.SetDate(Date);
}
#endregion
#region Methods
public void SetDate(Date Date)
{
this.Date = Date;
this.SetProperties();
}
private void SetProperties()
{
this.Properties.Add('d', this.Date.Day + "");
this.Properties.Add('m', this.Date.Month + "");
this.Properties.Add('Y', this.Date.Year + "");
this.Properties.Add('y', this.Date.Year.ToString().Substring(Math.Max(0, this.Date.Year.ToString().Length - 2)));
this.Properties_Count = Properties.Count;
}
public string Format(string FormatString)
{
int len = FormatString.Length;
if (Properties.ContainsKey(FormatString[0]))
{
FormatString = FormatString.Replace(FormatString[0] + "", this.Properties[FormatString[0]] + "");
}
for (int i = 1; i < len; i++)
{
if (this.Properties.ContainsKey(FormatString[i]) && FormatString[i - 1] != '\\')
{
FormatString = FormatString.Replace(FormatString[i] + "", this.Properties[FormatString[i]] + "");
}
}
return FormatString;
}
#endregion
}
내 문제 : 나는 각각의 새로운 DateHandler
에 새 사전을 정의해야하고, 생각하려고 해요 독창적 인 방법으로, 사전 중 하나만이 일치 정의를 가리킬 것입니다. 어떤 아이디어?
내 주요 목표하십시오 DateHandler
의 여러 인스턴스에서 값을 참조로 사용됩니다 사전 Properties
의 하나의 인스턴스입니다.
명확히하기 위해 모든 항목 (예 : 'd', this.Date.Day + "")이 포함 된 사전 인스턴스 (예 : '등록 정보'사전) DateHandler '가 참조 할 것입니까? :) 만약 내가 벗어났다면 알려주세요. –
'DateTime'을 저장하고 .NET 프레임 워크에 이미 존재하는 문자열 포맷 함수에 내장 된 특별한 이유가 있습니까? –
@Layoric 바로. – Novak