反射による静的クラスの汎用メソッドの実行
2813 ワード
今日会社でコードを書いている間に問題が発生しました。
呼び出されたコードは次のとおりです。
public static class CatalogComposition
{
private static AggregateCatalog catalogs = new AggregateCatalog();
public static AggregateCatalog AggregateCatalog
{
get { return catalogs; }
}
public static void ComposeParts<T>(T t)
{
CompositionContainer compositionContainer = new CompositionContainer(AggregateCatalog);
try
{
compositionContainer.ComposeParts(t);
}
catch (CompositionException ce)
{
throw new CompositionException(ce.Message, ce.InnerException);
}
}
}
エラーが修正されない前の反射コードを次に示します。
赤いコードはエラーコードです.
放出された例外は次のとおりです.
ContainsGenericParametersがTrueのタイプまたはメソッドに対して後期バインド操作を実行できません.
try
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
Type catalogComposition = Assembly.Load(ManifestConst.ARCHSTAR_FRAMEWORK_MEF).GetType(ManifestConst.ARCHSTAR_FRAMEWORK_MEF_CATALOGCOMPOSITION);
MethodInfo composePartsMethod = catalogComposition.GetMethod("ComposeParts", BindingFlags.Static | BindingFlags.Public);
composePartsMethod.Invoke(catalogComposition, new object[] { viewModel });
}
catch (ArgumentNullException ane)
{ }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
次は正しいコードです。
protected void ComposeParts(V viewModel)
{
try
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
Type catalogComposition = Assembly.Load(ManifestConst.ARCHSTAR_FRAMEWORK_MEF).GetType(ManifestConst.ARCHSTAR_FRAMEWORK_MEF_CATALOGCOMPOSITION);
MethodInfo composePartsMethod = catalogComposition.GetMethod("ComposeParts", BindingFlags.Static | BindingFlags.Public);
composePartsMethod.MakeGenericMethod(new Type[] { typeof(V) }).Invoke(catalogComposition, new object[] { viewModel });
}
catch (ArgumentNullException ane)
{ }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static class CatalogComposition
{
private static AggregateCatalog catalogs = new AggregateCatalog();
public static AggregateCatalog AggregateCatalog
{
get { return catalogs; }
}
public static void ComposeParts<T>(T t)
{
CompositionContainer compositionContainer = new CompositionContainer(AggregateCatalog);
try
{
compositionContainer.ComposeParts(t);
}
catch (CompositionException ce)
{
throw new CompositionException(ce.Message, ce.InnerException);
}
}
}
try
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
Type catalogComposition = Assembly.Load(ManifestConst.ARCHSTAR_FRAMEWORK_MEF).GetType(ManifestConst.ARCHSTAR_FRAMEWORK_MEF_CATALOGCOMPOSITION);
MethodInfo composePartsMethod = catalogComposition.GetMethod("ComposeParts", BindingFlags.Static | BindingFlags.Public);
composePartsMethod.Invoke(catalogComposition, new object[] { viewModel });
}
catch (ArgumentNullException ane)
{ }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
protected void ComposeParts(V viewModel)
{
try
{
if (viewModel == null)
{
throw new ArgumentNullException("viewModel");
}
Type catalogComposition = Assembly.Load(ManifestConst.ARCHSTAR_FRAMEWORK_MEF).GetType(ManifestConst.ARCHSTAR_FRAMEWORK_MEF_CATALOGCOMPOSITION);
MethodInfo composePartsMethod = catalogComposition.GetMethod("ComposeParts", BindingFlags.Static | BindingFlags.Public);
composePartsMethod.MakeGenericMethod(new Type[] { typeof(V) }).Invoke(catalogComposition, new object[] { viewModel });
}
catch (ArgumentNullException ane)
{ }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}