I'm trying to update a project which makes heavy use of comparison against SyntaxToken.Kind. This property appears to have disappeared in newer versions of Roslyn and I wondered if there an alternative method or an extension method I could write to get the same functionality?
The code has many references such as:
if (expression.OperatorToken.Kind == SyntaxKind.PlusEqualsToken)
Any ideas?
Add a using for Microsoft.CodeAnalysis.CSharp.Syntax, and then use the CSharpKind() extension method.
Related
I have just updated my project from .NET Standard 2.0 to 2.1 (Due to a Unity3D Version Update), and with this update, a lot of methods that I previously implemented with extension methods are now built-in to the type itself. (e.g. TryGetValue for KeyedCollection)
Unfortunately, some of my extension methods share the same name as the C# implementation, but have different behaviours. (e.g. my TryGetValue won't throw an exception when a null key is given, but .Net 2.1's will throw). As such, I am having a lot of runtime errors.
I wonder if there's any way to get the compiler to generate a warning for all of the extension methods that conflict with the base class?
Currently, I am finding all extension methods through finding all (this , and then checking if it has any usages (Code metrics display from Rider)
But this is taking a really long time and I want to see if there's a better way.
I'm using Visual Studio 2015 and C#. Sometimes I have methods which are using by reflection or serialization and in warnings window I have information that method is not using. Is problem because while someone try refactoring code getting errors in runtime. Is any possibility to inform editor that method is using? Maybe some attribute for that (it will be the best solution)? Any ideas?
As an author of code, you cannot tell whether a method is being executed via Reflection. Because Reflection is late-bound, the compiler also cannot tell what method (if any) might be called by a Reflection Invoke.
It seems like there are methods and properties of ObservableCollection that are missing. I'm so confused.
Here is a screenshot of an old project:
And now my current project:
This is the using statement in both projects:
using System.Collections.ObjectModel;
Did they update the SDK and removed all of the methods or am I using the wrong ObservableCollection? I have no idea what's going on.
When you see the down arrow on a method that means that method is not part of the class but is a "Extension Method". Extension methods are basically static methods from other classes that act like instance methods on the class you are working with.
Most of the items in your list are from adding using System.Linq; to the top of your file. This causes all of the extension methods in System.Linq.Enumerable to show up in the list, this will give you things like All<> or Any<>. However AddRange<> is not a standard extension method in System.Linq and may be added by some other 3rd party library (or some namespace in .NET I am not aware of) that you are using.
The easiest way to find out where you are getting AddRange<> from is go to the project that it works for and then right click on the method in code and you should see a "Go To Definition" or similar1, that should take you to the file or the metadata view of the file that declared that extension method.
1: I use Resharper and it changes my right click menu so I don't know the exact wording
I think you're missing the LINQ extensions. Try adding:
using System.Linq;
The "missing" methods are LINQ extension methods. Add LINQ to your list of included libraries.
Is there a way to use reflection to completely "scan" an assembly to see if System.IO.File or System.IO.Directory is ever used? These are just example classes. Just wondering if there is a way to do it via reflection (vs code analysis).
update:
see comments
As Tommy Carlier suggested, it's very easy to do with Cecil.
using Mono.Cecil;
// ..
var assembly = AssemblyFactory.GetAssembly ("Foo.Bar.dll");
var module = assembly.MainModule;
bool references_file = module.TypeReferences.Contains ("System.IO.File");
The fantastic NDepend tool will give you this sort of dependency information.
Load your dll in NDepend and either use the GUI to find what you want, or the following CQL query:
SELECT TYPES WHERE IsDirectlyUsing "System.IO.File"
and you should get a list of all the types that use this.
I'd suggest looking at Mono Cecil for this. With Cecil, you can enumerate all the classes, methods and even the IL-instructions (including all the methods calls).
I don't remember where, but I found this handy piece of code:
http://gist.github.com/raw/104001/5ed01ea8a3bf7c8ad669d836de48209048d02b96/MethodBaseRocks.cs
It adds an extension method to MethodInfo/ConstructorInfo that parses the ILByteArray into Instruction objects.
So with this, you could loop over every MethodInfo/ConstructorInfo in the assembly, then loop over every Instruction on that MethodInfo/ConstructorInfo, and check if any of those Instruction objects contains an Operand which is an instance of a MemberInfo which has a DeclaringType that is equal to either class.
You can get a list of dependent assemblies via Assembly.GetExecutingAssembly().GetReferencedAssemblies(). I don't believe you can comprehend namespace usage via reflection. Try looking at System.CodeDom. That may help you parse the code.
.NET Reflector can do this, or something close to it. The other day I checked to see where a particular type was used.
ReSharper might also help. I do this with my own symbols all the time - I suppose it would also work for .NET Framework types.
I have a method called FormattedJoin() in a utility class called ArrayUtil. I tried renaming FormattedJoin() to just Join() because it's behavior is similar to .NET's string.Join() so I figured it made sense to use the same name. However, when I try to use Visual Studio to rename the method, I get this warning:
This member may have compiler generated references
with the same name. Refactoring the member will not
update these references, which may introduce semantic
changes and/or build errors into your code.
I can rename the method just fine and it causes no build errors or compiler warnings. Is it safe to do this or should I avoid having a method with this name?
After seeing this error, I opened up Reflector to see if I could find out if .NET had an internal "ArrayUtil.Join()" or any variation of that and it doesn't look like it. Even if there was an "ArrayUtil.Join()" method though, wouldn't having a different namespace make this a non-issue?
This has to do with LINQ.
The C# compiler will generate calls to a method named Join when you use the "Join" keyword in a LINQ query. Usually the call gets resolved to one of the LINQ extension methods. If you define your own method with the same signatgure as the LINQ methods, however, the compiler will use yours. The purpose of the warning is to let you know that if you make additional refactoring that the compiler generated method calls will not be modified.
Unless you are trying to write a custom LINQ provider, it's safe to ignore the error.