2

WCF は型をサポートしていないため、型を文字列型として渡しています。例:

var str= "int"

CLR型をパラメーターとして渡したいので、これを Type intに変換したいと思います。

これを達成する方法はありますか?

4

3 に答える 3

3

Type.GetType()を使用するのが好きですか?

string typeName = "System.Int32"; // Sadly this won't work with just "int"
Type actualType = Type.GetType(typeName);
于 2012-07-19T10:42:29.887 に答える
2

型が現在実行中のアセンブリまたは Mscorlib.dll にある場合は、その名前空間によって修飾された型名を取得するだけで十分です ( @Rawling の回答を参照)。

var str = typeof(int).FullName;
// str == "System.Int32" 

それ以外の場合は、次のアセンブリ修飾名が必要ですType

var str = typeof(int).AssemblyQualifiedName;
// str == "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

次に、次を使用できますType.GetType

var intType = Type.GetType(str);

編集:

システム エイリアスを使用する場合は、 を作成して、Dictionary<string, Type>すべてのエイリアスをそのタイプにマップできます。

static readonly Dictionary<string, Type> Aliases =
    new Dictionary<string, Type>()
{
    { "byte", typeof(byte) },
    { "sbyte", typeof(sbyte) },
    { "short", typeof(short) },
    { "ushort", typeof(ushort) },
    { "int", typeof(int) },
    { "uint", typeof(uint) },
    { "long", typeof(long) },
    { "ulong", typeof(ulong) },
    { "float", typeof(float) },
    { "double", typeof(double) },
    { "decimal", typeof(decimal) },
    { "object", typeof(object) }
};
于 2012-07-19T10:51:40.787 に答える
0

これを試して

 int myInt = 0;
    int.TryParse(str, out myInt);

    if(myInt > 0)
    {
     // do your stuff here
    }

タイプのみを送信したい場合は、

string str = myInt.GetType().ToString(); そしてそれはあなたにタイプを与えるでしょう

于 2012-07-19T10:41:49.637 に答える