Skip to content

Instantly share code, notes, and snippets.

@pierre3
Last active December 21, 2015 12:39
Show Gist options
  • Save pierre3/6307212 to your computer and use it in GitHub Desktop.
Save pierre3/6307212 to your computer and use it in GitHub Desktop.
[C#][小ネタ] 文字列を各種組み込み型にキャスト(もどき)

こちらの記事から

bool dat = (bool:TextToBoolConverter)file[0];
こんな構文ないけど、型変換演算子のオーバーロードで、ほぼ同等ことができるのでは?ということで作ってみました。

他の主要な組み込み型についてもT4を使ってさくっと作れます。

(個人的にはToBoolean()みたいな拡張メソッドの方が使いやすいと思いますが。(インテリセンス使えますし))

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StringUtility;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = (bool)(StringCaster)"true";
            var y = (bool)(StringCaster)"FALSE";
            var z = (bool)(StringCaster)"TRUE";

            byte a = (StringCaster)"255";
            short b = (StringCaster)"-1000";
            ushort c = (StringCaster)"32768";
            int d = (StringCaster)"-99999999";
            uint e = (StringCaster)"2147483648";
            long f = (StringCaster)"-2";
            ulong g = (StringCaster)"9223372036854775808";
            float h = (StringCaster)"3.14";
            double i = (StringCaster)"6.02e-23";
            decimal j = (StringCaster)"1.4142";
            DateTime k = (StringCaster)"2013/08/22 18:30:25";
            bool l = (StringCaster)"TRUE";

            Console.WriteLine(x);
            Console.WriteLine(y);
            Console.WriteLine(z);
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.WriteLine(c);
            Console.WriteLine(d);
            Console.WriteLine(e);
            Console.WriteLine(f);
            Console.WriteLine(g);
            Console.WriteLine(h);
            Console.WriteLine(i);
            Console.WriteLine(j);
            Console.WriteLine(k);
            Console.WriteLine(l);

            Console.Read();
        }


    }
}

using System;
using System.Linq;
using System.Text;
using System.Globalization;
namespace StringUtility
{
public partial class StringCaster
{
public string Source { get; protected set; }
public StringCaster(string source)
{
this.Source = source;
}
//文字列をStringCasterにキャスト
public static implicit operator StringCaster(string s)
{
return new StringCaster(s);
}
//各Typeへキャスト
public static implicit operator byte(StringCaster caster)
{
return byte.Parse(caster.Source);
}
public static implicit operator short(StringCaster caster)
{
return short.Parse(caster.Source);
}
public static implicit operator ushort(StringCaster caster)
{
return ushort.Parse(caster.Source);
}
public static implicit operator int(StringCaster caster)
{
return int.Parse(caster.Source);
}
public static implicit operator uint(StringCaster caster)
{
return uint.Parse(caster.Source);
}
public static implicit operator long(StringCaster caster)
{
return long.Parse(caster.Source);
}
public static implicit operator ulong(StringCaster caster)
{
return ulong.Parse(caster.Source);
}
public static implicit operator float(StringCaster caster)
{
return float.Parse(caster.Source);
}
public static implicit operator double(StringCaster caster)
{
return double.Parse(caster.Source);
}
public static implicit operator decimal(StringCaster caster)
{
return decimal.Parse(caster.Source);
}
public static implicit operator DateTime(StringCaster caster)
{
return DateTime.Parse(caster.Source);
}
public static implicit operator bool(StringCaster caster)
{
return bool.Parse(caster.Source);
}
}
}
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Globalization" #>
<#@ output extension=".cs" #>
<#
var types = new []
{
"byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "decimal", "DateTime", "bool"
};
#>
using System;
using System.Linq;
using System.Text;
using System.Globalization;
namespace StringUtility
{
public partial class StringCaster
{
public string Source { get; protected set; }
public StringCaster(string source)
{
this.Source = source;
}
public static implicit operator StringCaster(string s)
{
return new StringCaster(s);
}
<# foreach(var typeName in types){ #>
public static implicit operator <#= typeName #>(StringCaster caster)
{
return <#= typeName #>.Parse(caster.Source);
}
<# } #>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment