In my project I have Resharper installed, and I’m doing design time templating with T4 Templates from within Visual Studio.
I have
<#@ assembly name="$(SolutionDIr)FTest\bin\Debug\FTest.dll" #>
<#@ assembly name="$(SolutionDIr)FTest\bin\Debug\nunit.framework.dll" #>
I also have
<#@ import namespace="NUnit.Framework" #>
Then i have a cast in the code like this
<#
var someVar = (TestAttribute)typeof(BaseTest).GetMethods()
.Where(
x => x.GetCustomAttributes(false).Where(y => y.Name == "CategoryAttribute" && ((CategoryAttribute)y).Name == "Smoke").Any()
)
#>
Im getting an invalid cast exception. It tells me that the template generator uses a different ‘nunit.framework’ dll than the project dll uses.
Error 21 Running transformation: System.InvalidCastException: [A]NUnit.Framework.CategoryAttribute cannot be cast to [B]NUnit.Framework.CategoryAttribute. Type A originates from 'nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' in the context 'LoadFrom' at location 'C:\Users\Chandirasekar Thiaga\AppData\Local\assembly\dl3\ZGE1068O.OD1\ARWGXOXD.EMZ\7407d57d\ff3a4298_6ceccc01\nunit.framework.dll'. Type B originates from 'nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' in the context 'Default' at location 'C:\Program Files (x86)\JetBrains\ReSharper\v6.1\Bin\nunit.framework.dll'.
How do I, when importing with <#@ import namespace=”NUnit.Framework” #> import the same dll as the one that FTest.dll uses? I do not want to use the Resharper’s version of nunit!
EDIT :
After setting registry key like GarethJ said, the message looks like this :
Error 12 Running transformation: System.InvalidCastException: [A]NUnit.Framework.CategoryAttribute cannot be cast to [B]NUnit.Framework.CategoryAttribute. Type A originates from 'nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' in the context 'LoadFrom' at location 'C:\Repos\BSF-Functional-Automation\FunctionalTest\FunctionalTest\bin\Debug\nunit.framework.dll'. Type B originates from 'nunit.framework, Version=2.5.10.11092, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77' in the context 'Default' at location 'C:\Program Files (x86)\JetBrains\ReSharper\v6.1\Bin\nunit.framework.dll'.
at Microsoft.VisualStudio.TextTemplating64EAA000670725A96AF52252D093BE63.GeneratedTextTransformation.<TransformText>b__5(Object x)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.VisualStudio.TextTemplating64EAA000670725A96AF52252D093BE63.GeneratedTextTransformation.<TransformText>b__4(MethodInfo m)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.VisualStudio.TextTemplating64EAA000670725A96AF52252D093BE63.GeneratedTextTransformation.TransformText()
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.RunTransformation(TemplateProcessingSession session, String source, ITextTemplatingEngineHost host, String& result) 1 1
The @import just contributes a ‘using’ statement rather than a reference, so it’s not consequential here.
Type A loaded in the LoadFrom context is the one that is being loaded by the assembly tag in T4.
Type B that’s coming from the Resharper directory appears from this error to be coming from the reflection over the assembly that’s containing ‘BaseTest’.
I’d try looking at the build of BaseTest to see if Resharper could have crept in here. You can probably validate this theory by running reflector or ildasm over basetest’s assembly.