私はリフレクションとジェネリックメソッドの使用経験がありません。ここには2つのメソッドがあります。私はあなたがやろうとしていることを理解できると思います。C#reflection:ジェネリックメソッドのTの提供
public static T GetInHeaderProperty<T>() where T : new()
{
dynamic result = new T();
result.CompanyId = ConfigurationManager.AppSettings["CompanyId"];
result.UserId = ConfigurationManager.AppSettings["UserId"];
result.Password = ConfigurationManager.AppSettings["Password"];
result.MessageId = ConfigurationManager.AppSettings["MessageId"];
Type platformType = typeof(T).GetProperty("PlatformType").PropertyType;
// Here is my problem, I can not compile my code because of this line
result.PlatformType = (dynamic)GetPlatformType<platformType>();
//-------------------------------------------------------------------
return (T)result;
}
public static T GetPlatformType<T>() where T : struct
{
string platform = System.Configuration.ConfigurationManager.AppSettings["Platform"];
T value;
if (Enum.TryParse(platform, out value))
return value;
else
return default(T);
}
私はコンパイル時に次のエラーを取得しています:
型または名前空間名「platformType」が見つかりませんでした(あなたがusingディレクティブまたはアセンブリ参照が不足しています?)。
このメソッドはどのように呼び出すことができますか?
ありがとうございます。
ランタイムまで型がわからない場合は、おそらくジェネリックが必要ないのですか? – Jamiec
同じ問題を抱えている人を助けるために質問を少し調整してください。選択したアンカーは間違っていませんが、一般的な質問ではなくコードに一致します。 –