26
namespace Myspace
{
    public class MyClass
    {
    }
} //This class is in another file.

using Myspace;
static void Main(string[] args)
{
    Regex regexViewModelKey = new Regex(RegularExpr.ViewModelKeyPattern);
    string viewModel = regexViewModelKey.Match(match.Value).Value;
    //Now, vieModel is a string, and its value is "MyClass". So, I only know the class name, this is why I ask this question.

    //Now, I'm only allowed to use the string form of this class name to get its type.
    //I have tyied like this, but its no use.
    Type t = Type.GetType(viewModel);
    //it's return a null.

    //Then I tyied another method like this, but there is an exception when calling Assembly.Load
    Assembly assembly = Assembly.Load("Myspace");
    Type ty = assembly.GetType("Myspace" + viewModel);
}

私の質問が明確であることを願っています。誰でも私を助けることができます.THX私は、このクラス名の文字列形式を使用してその型を取得することしか許可されていません。

皆さん。私はこのようにこの質問を自分で解決しました。

{
      Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
}
4

3 に答える 3

51

関数 typeof() を使用するだけです。パラメータはそのクラス名です。

Type type = typeof(FIXProtoClientTest);

typeof() に関する MSDN

于 2015-10-11T19:43:17.180 に答える