2017-08-18 7 views
0

私はmoqを使用してクラスを内部コンストラクタでモックしようとしました。法による内部コンストラクタでMoqアクセスメソッドが失敗しました

System.MethodAccessException 試み「Castle.Proxies.MyClassProxy..ctor(Castle.DynamicProxy、すなわち、私の以前のpostに続き、私はコンストラクタを打つように見えるが、今新しいエラーに直面して.IInterceptor []) ' メソッドにアクセスする' MyAssembly.MyClass..ctor() 'が失敗しました。 Castle.Proxies.StageProxy..ctor(IInterceptor []) ---内部例外スタックトレースの終了--- at System.RuntimeMethodHandle.InvokeMethod(Object target、Object []> arguments、Signature sig、Booleanコンストラクタ)バインドされたバインダー、オブジェクト[] args、CultureInfo culture、Object [] System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr、バインダーバインダー、バインダーバインダー、オブジェクト[]パラメーター、CultureInfoカルチャ) でSystem.Reflection.RuntimeConstructorInfo.Invoke (Type型、Object []、System.Activator.CreateInstance)。 at System.Activator.CreateInstance(Type型、BindingFlags、bindingAttr、バインダーバインダー、Object [] args、CultureInfoカルチャ、Object [] activationAttributes) args)012 Moq.PexProtector.Invoke(アクションアクション) でCastle.DynamicProxy.ProxyGenerator.CreateClassProxyInstance(タイプproxyTypeので、リスト1 proxyArguments, Type classToProxy, Object[] constructorArguments) at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) at Moq.Proxy.CastleProxyFactory.CreateProxy(Type mockType, ICallInterceptor interceptor, Type[] interfaces, Object[] arguments) at Moq.Mock 1.b__20_0() Moq.Mock`1でMoq.Mock 1.InitializeInstance() at Moq.Mock 1.OnGetObject() で。 TestAssembly.UnitTest1.TestMethod1でGET_OBJECT() は、()はC:\ユーザーは、プロジェクト\ TestMoqInternals \ TestAssembly \ UnitTest1.cs \ Visual Studioの2017 \ briaris-jは\ドキュメントを\:行16

これが私のAssemblyInfo.csですMyAssembly、すなわち私の内部型を含むアセンブリ

using System.Reflection; 
using System.Runtime.CompilerServices; 
using System.Runtime.InteropServices; 

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information 
// associated with an assembly. 
[assembly: AssemblyTitle("MyAssembly")] 
[assembly: AssemblyDescription("")] 
[assembly: AssemblyConfiguration("")] 
[assembly: AssemblyCompany("")] 
[assembly: AssemblyProduct("MyAssembly")] 
[assembly: AssemblyCopyright("Copyright © 2017")] 
[assembly: AssemblyTrademark("")] 
[assembly: AssemblyCulture("")] 

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type. 
[assembly: ComVisible(false)] 

// The following GUID is for the ID of the typelib if this project is exposed to COM 
[assembly: Guid("2fda4fb1-6855-4af5-a0a3-fbe861dcc734")] 

// Version information for an assembly consists of the following four values: 
// 
//  Major Version 
//  Minor Version 
//  Build Number 
//  Revision 
// 
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below: 
// [assembly: AssemblyVersion("1.0.*")] 
[assembly: AssemblyVersion("1.0.0.0")] 
[assembly: AssemblyFileVersion("1.0.0.0")] 

// Allow moq to access internal constructors of entities 
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] 

これはどのように修正することができますか?

+1

'InternalsVisibleTo'属性を追加しましたか? – CodeNotFound

+1

はい、私が追加しました: '[アセンブリ:InternalsVisibleTo( "DynamicProxyGenAssembly2、のPublicKey = 0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]' [ドキュメント](https://github.com/Moq/moq4/wiki/Quickstart#advanced-features)に従って –

+0

奇妙な。あなたのエラーは、コンストラクターへのアクセスが失敗したため、正しいアセンブリにアトリビュートを入れなかったと思います。 – CodeNotFound

答えて

1

ので

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] 

から

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2"] 

に私のInternalsVisibleTo属性を変更することはトリックを行っているようです。奇妙なオリジナルはmoqから直接コピーされましたdocs

関連する問題