ジェネリックは型を抽象化することでメリットがでます。
ジェネリック・ルーチン内で型判断して、処理を分岐するのに違和感感じるのは私が変なのか?
Castができないから ((str_str)(object)ss) とするのは、強引すぎる感じがするのは私だけ?
確かに ((str_str)ss) とするとCompileErrorになるが、((str_str)(object)ss)とするとコンパイルできる。......うーん。いいのかもしれないが。
public List<T> get_Analize<T>() where T : new()
{
List<T> rc = new List<T>();
string pat = null;
if (typeof(T) == typeof(str_str)) pat = @"(.*)\((.*)\)";
if (typeof(T) == typeof(decimal_str)) pat = @"([-|+|\d|\.]*)(?<=\d)(.*)";
Regex r = new Regex(pat, Option.ig);
Match m = r.Match(_src);
while (m.Success)
{
T ss = new T();
if (typeof(T) == typeof(str_str))
{
((str_str)(object)ss).str1 = m.Groups[1].Value;
((str_str)(object)ss).str2 = m.Groups[2].Value;
rc.Add(ss);
}
if (typeof(T) == typeof(decimal_str))
{
decimal d = 0;
if (decimal.TryParse(m.Groups[1].Value, out d))
{
((decimal_str)(object)ss).dec = d;
((decimal_str)(object)ss).str = m.Groups[2].Value;
rc.Add(ss);
}
}
m = m.NextMatch();
}
return rc;
}