On one of our solutions, with Code Analysis (CA) turned on, we get the error:
CA0001 : Member 'FirstOrDefault' could not be found in type 'System.Linq.Enumerable' in assembly '[our project]'.
Looking on the internet ( http://msdn.microsoft.com/en-us/library/ms245246.aspx ), Microsoft tells us to look in the CodeAnalysisReport.xml file. But nowhere on my machine a file like that exists.
Also the msdn page tells us to look into custom rules, but we do not have any custom rules.
Can someone please get us into the right direction how to fix this? Or how to get some more information about this strange behavior of CA?
Edit: #25 apr 2012 - 10:38
After some investigation of the output of VS2010, i found a file [mydll].CodeAnalysisLog.xml in the bin/debug directory. There the error is more specified:
<Exception Keyword="CA0001" Kind="AssemblyLoad">
<Type>Microsoft.FxCop.Common.AssemblyLoadException</Type>
<ExceptionMessage>Member 'FirstOrDefault' could not be found in type 'System.Linq.Enumerable' in assembly 'Prodigy.UI.Modules.SecurityDesigner.Silverlight, Version=0.0.0.1, Culture=neutral, PublicKeyToken=fdb1b3fc35f0c32e'.</ExceptionMessage>
<StackTrace> at Microsoft.FxCop.Engines.Phoenix.LoadAndResolutionHandler.HandleFailureToFindMatch(ModuleUnit unit, String memberName, Type memberType, String className, UInt32 memberRefToken, UInt32 classToken)
at Phx.Metadata.ReaderImplementation.GetExternalDefinitionMemberSymbolHelper(UInt32 memberToken, String name, Byte* signature, Symbol parentSymbol, Symbol originalParentSymbol)
at Phx.Metadata.ReaderImplementation.GetExternalDefinitionMemberSymbolHelper(UInt32 memberToken, String name, Byte* signature, Symbol parentSymbol, Symbol originalParentSymbol)
at Phx.Metadata.LoaderImplementation.GetExternalDefinitionMemberSymbol(UInt32 memberToken)
at Phx.Metadata.LoaderImplementation.GetMemberReferenceSymbol(UInt32 memberReferenceToken)
at Phx.Metadata.MetadataLoader.LoadMemberReferences(AssemblyUnit unit)
at Microsoft.FxCop.Engines.Phoenix.AssemblyLoader.LoadAssembly(String filePath)
at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.AnalyzeInternal()
at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.Analyze()
at Microsoft.FxCop.Common.EngineManager.Analyze(Project project, Boolean verboseOutput)</StackTrace>
</Exception>
But still the question remains, how to fix this?
I recently stumbled onto this problem and after traditional headbashing, the solution was quite funny. The breaking change was referencing a custom portable library in my project. This effectively brought two sets of system references in the result. Meaning System.Core was referenced twice as 4.0.0.0 and 2.0.whatever. And looks like fxcop is not smart enough to handle that (the code was working, it's really validation only).
tl;dr Check what is effectively referenced by your troubled assembly. make sure no overlaps-replicants have made into your assembly.
Check into your code, if the member 'FirstOrDefault' in type 'System.Linq.Enumerable' in assembly '[mydll].Silverlight, Version=0.0.0.1, Culture=neutral, PublicKeyToken=fdb1b3fc35f0c32e'?
Through the error description, it seems that you have a "System.Linq.Enumerable" class in the [mydll].Silerlight.dll assembly, is this right?
Snippet from http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/c2165692-67ea-4b4a-b730-52f4b4ff0313/
Related
Is there Visual Studio warning for the common GCC error,
non-const lvalue reference to type
I would like to make sure that such a coding paradigm is found, and an error thrown. According to a couple answers, already asked on SO, it sounds like it is just part of the MSVC++ compiler "extension". That is, there is no direct number.
My goal is to do something like,
/weXYZW
You might be looking for /Zc:referenceBinding
I have this question on my review sheet that I cant seem to get but rather than ask you for the answer I would rather like to learn the difference between these specific concepts.
For reference the question is An LC-3 instruction ADD R1,R2, #45 produces an error. It will be caught at a. assembly time b. link time c. run time d. compile time. Rather than just finding out the answer what would rather like to know is what is the difference between these and how do they differ when it comes to error handling?
Using the C programming language as an example the 4 steps to create an executable program Preprocessing, Compiling, Assembling, and Linking.
Compile time
These are generally common and are caused by a malformed user program that the compiler can't process, things such as forgetting a semicolon can cause a compiler error.
Assembly Time
Something went wrong with the assembler. This includes using instructions incorrectly as above, not defining a LABEL but using it in an instruction etc.
Link Time
As part of the C compile process to form an executable many object files generated by the assemble step are linked together. In C programming you can specify that some symbol is defined externally via the extern keyword, other things like function prototypes will tell the compiler a function is defined somewhere.
The linker will resolve where those variables/functions live. If you haven't declared a function/variable and something references it then you will get a undefined reference error. Same for if something is defined multiple times.
Run Time
An error occurred during running your program, this is something such as accessing a pointer that is null, or dividing by zero.
I want to use a COM object in my VS2012 project. For these purposes I was given an IDL file that describes the object. I have used MIDL to generate a header file which looks (partly) like this:
EXTERN_C const CLSID CLSID_COComponent;
#ifdef __cplusplus
class DECLSPEC_UUID("bla bla bla")
COComponent;
#endif
However, when I include this header into my project, and try to use CLSID_COComponent in a CoCreateInstance call:.
CComPtr<IModelService> m_IModelService;
hRes = m_IModelService.CoCreateInstance(CLSID_COComponent, NULL, CLSCTX_ALL);
I get an linker error:
Error 123 error LNK2001: unresolved external symbol _CLSID_COComponent
However if I use __uuidof(COComponent), it works fine. Also when removing the EXTERN_C from the generated code, it compiles.
The exact same code has been used in a different project, where I have seen it work. My question is, what could I be doing wrong?
The exact same code has been used in a different project, where I have seen it work. My question is, what could I be doing wrong?
That means, there's nothing wrong with the code itself. It has to be something external to the code. The hint is in the CLSID declaration:
EXTERN_C const CLSID CLSID_COComponent;
EXTERN_C expands to extern "C", so the full statement is telling the compiler that there is a symbol called CLSID_COComponent of type const CLSID, but it is defined someplace else. Don't try to look it up, have the linker stitch the pieces together later on.1
The definition for the CLSID is also generated by MIDL. It's placed in an Interface UUID File. The default name for this file is <idl base name>_i.c (can be overridden with the /iid MIDL switch). You have to include that file into your project, so that the linker will find the symbol.
Alternatively, you can use the __uuidof operator, that doesn't need the Interface UUID File. I cannot tell you, which compiler magic makes this works. But if Hans Passant calls it a "valid workaround", I'm not going to question that.
1 Removing the EXTERN_C allows this to compile, by defining the symbol. It will fail at runtime, though, since the CLSID doesn't store the correct value.
This is extremely strange, and I'm hoping someone will have some insights to make sense of this.
I have an F# 2.0 (Visual Studio 2010, targeting .Net 4.0) solution which works fine in the location where I originally created it, but if I try and copy it to a new folder (because I want to check it in to source control), I get some very odd errors when building. They tend to be along the lines of:
error FS0803: Invalid use of a type name and/or object constructor. If necessary use 'new' and apply the constructor to its arguments, e.g. 'new Type(args)'. Overloads are: None() : unit.
or
error FS0001: This expression was expected to have type obj option but here has type Some<'a>
These errors are only occurring for uses of the option type, a simple example of one such usage being:
let asOption e =
match e with
| null -> None
| _ -> Some(e)
Now, remember, this is a solution that compiles just fine in its original location. I've tried the obvious like Clean/Rebuild, deleting the obj and bin directories, restarting Visual Studion, and still, the same.
The reference DLLs are all the same in both cases, GAC'd DLLs are being referenced from the GAC, non-GAC'd dlls are copied and being referenced from the same relative path. Just for fun, I've even compared the output window text of the calls to Fsc.exe used to compile each solution to ensure the compiler is being called with the same arguments in both cases, and, naturally, it is.
Anyone have any idea of what may be causing this? Am I getting some strange limbo version of FSharp.Core.dll out of the GAC somehow? Am I just the most unlucky of the unlucky stiffs?
So the weird symptom had an equally weird cause.
It turns out one of the DLLs I was referencing (the ubiquitous "Core" junk-drawer dll every project has) must have some extension methods in place that are causing some issues with the type inference used by the compiler and visual studio.
When I remove the open MyProject.Core and replace any of the references to types I'm using with the fully qualified name, the strange errors magically go away.
So, at this point, two questions remain:
What kind of insane extension methods are in there that could be causing this
Why was the original solution/project unaffected by this? (I'm guessing this may be related to the order in which the references were passed into the call to Fsc.exe...but I'm not sure).
I may actually dig in enough to try and figure out the answer to #1. I'm not sure if it is exposing some sort of bug with the F# compiler (doubtful), or if my co-workers are just doing something unnatural with extension methods (highly likely)
UPDATE:
Looks like someone had some F# envy and created an Option<T> class in the Core project. Pretty much explains things. I've not experimented to see if the order of the includes on the Fsc.exe call make a different, but I have a feeling that is it.
References should almost never come from the GAC, they should come from Reference Assemblies. Post the fsc.exe command-line you are witnessing, perhaps it will shed more light. I am guessing the FSharp.Core reference is messed up somehow (perhaps related to the sigdata/optdata files that stand alongside its reference assembly?)
(Points for a very weird symptom, though!)
Assuming F# compiler is deterministic, there obviously must be some difference in how it is being invoked, perhaps in the environment variables. Have you tried running MSBuild from the command line? It usually helps with these kind of things.
One guess of what's going on is that F# infers different types for your code. Have you tried constraining it a bit to only have to deal with simple types? For example:
let asOption (e: obj) =
match e with
| null -> None
| _ -> Some(e)
Or else:
let asOption<'T> (x: 'T) : option<'T> =
if x :> obj = null then None else Some x
The later gets rid of the null constraint on the generic parameter.
How does a compiler detect duplicate definition across translation unit. Suppose there were a extern const variable declaration in an header file. If this header file was used in more than one translation unit - each having a separate definition - each TU object creation would be successful, however when the final executable is created the error is thrown.
Is there a reference table created to account these duplication while linking each of these TU (during the creation of the executable)?
Any link on this topic would be helpful.
Thanks in advance for your explanation.
Normally this would be detected by the linker, rather than the compiler. The linker can then either coalesce the variables (often required for sloppy C/C++ coding) or report an error.
Yes, the linker builds a list of unresolved external references and then eventually goes on to attempt to resolve them one by one.