How to enable C# 7 features in existing projects - visual-studio-2017

I am not sure how to use new C# 7 features in existing solution. I tried using pattern matching in a switch statement but I keep getting Value of integral type expected error.
Is there a trick to enable it? I though I can just use new features if I open the solution in VS 2017.
My projects are targeting .net 4.6.2.
Here is the sample code
private void CS7Test(object o)
{
switch (o)
{
case null:
Console.WriteLine("it's a constant pattern");
break;
case int i:
Console.WriteLine("it's an int");
break;
case UserInfo p when p.Username.StartsWith("Ka"):
Console.WriteLine($"a Ka person {p.Username}");
break;
case UserInfo p:
Console.WriteLine($"any other person {p.Username}");
break;
case var x:
Console.WriteLine($"it's a var pattern with the type {x?.GetType().Name} ");
break;
default:
break;
}
}

Actually it's working without doing any special config. Resharper was giving those errors and after disabling the resharper it worked like a charm.

Vs 2017 is configured by default to support c#7.0 with enabling Resharper.
You need not to disable it.
In vs 2017 update 3 you can configure it to use the new features of c# 7.1 (and resharper is enabled also).
For more details review: setting c#7.1 in vs2017.3

Related

Eclipse CDT: Problems with extension point CIndexer

Problem 1: I can't find org.eclipse.cdt.core.index.IIndexer
From the API:
API Information: Plug-ins that want to extend this extension point must implement org.eclipse.cdt.core.index.IIndexer interface.
Is the API Information incorrect/deprecated? Which interface should be implemented if not IIndexer?
Problem 2: I can install my own Indexer in CDT version 6.8 (eclipse 2019-06) but not in version 6.5 (eclipse 2018-09), though I don't see the difference in the plugin code.
More Details:
My Indexer class:
#SuppressWarnings("restriction")
public class MyIndexer extends PDOMFastIndexer {
public static final String ID = "de.blub.MyIndexer";
#Override
public String getID() {
return ID;
}
#Override
public IPDOMIndexerTask createTask(ITranslationUnit[] added, ITranslationUnit[] changed,
ITranslationUnit[] removed) {
if (...) {
return new MyIndexerTask(added, changed, removed, this, true);
} else {
return super.createTask(added, changed, removed);
}
}
The plugin.xml
<extension
id="org.eclipse.cdt.core.fastIndexer"
name="My Indexer"
point="org.eclipse.cdt.core.CIndexer">
<run
class="de.blub.MyIndexer">
</run>
The MANIFEST.MF File lists org.eclipse.cdt.core in the Require-Bundle section without bundle-version. Of course the cdt plugin has different versions:
In Eclipse 2019-06:
Eclipse CDT C/C++ Development Tools Core 6.8.1.201907021957 org.eclipse.cdt.core
In Eclipse 2018-09:
Eclipse CDT C/C++ Development Tools Core 6.5.0.201811180605 org.eclipse.cdt.core
This code is from org.eclipse.cdt.internal.core.pdom.PDOMManager:
private IPDOMIndexer newIndexer(String indexerId, Properties props) throws CoreException {
IPDOMIndexer indexer = null;
// Look up in extension point
IExtension indexerExt = Platform.getExtensionRegistry().getExtension(CCorePlugin.INDEXER_UNIQ_ID, indexerId);
if (indexerExt != null) {
IConfigurationElement[] elements = indexerExt.getConfigurationElements();
for (IConfigurationElement element : elements) {
if ("run".equals(element.getName())) { //$NON-NLS-1$
try {
indexer = (IPDOMIndexer) element.createExecutableExtension("class"); //$NON-NLS-1$
indexer.setProperties(props);
} catch (CoreException e) {
CCorePlugin.log(e);
}
break;
}
}
}
// Unknown index, default to the null one
if (indexer == null)
indexer = new PDOMNullIndexer();
return indexer;
}
The code is the same for both cdt versions. indexer becomes a PDOMFastIndexer in eclipse 2018-09, but a MyIndexer in 2019-06.
One difference I could see is that in RegistryObjectManager
private Object basicGetObject(int id, byte type) {
Object result = cache.get(id);
if (result != null)
return result;
...
}
An id is used to get the correct ConfigurationElement (result) from a cache object, which I don't really understand how it is built up. However, the returned ConfigurationElement contains a field propertiesAnsValues which is incorrect in the one case (org.eclipse.cdt.internal.core.pdom.indexer.PDOMFastIndexer instead of de.blub.MyIndexer).
How can I fix that to have my own indexer in eclipse 2018-09, too?
Please also note my Problem 1. Because if the API description is correct, it means I'm trying to install my indexer the wrong way and need to do something to 'see' the IIndexer interface.
According to the schema definition, the class you need to derive from is IPDOMIndexer (which you're already doing). You can also tell this from the PDOMManager code you quoted, which casts the result of createExecutableExtension() to IPDOMIndexer.
(The comment saying to use org.eclipse.cdt.core.index.IIndexer is indeed out of date. Based on a brief look, that interface hasn't existed since at least 2005. Patches to update the extension point documentation are welcome.)
As for your second problem, I believe it's because you're using id="org.eclipse.cdt.core.fastIndexer" for your extension, which is already in use by one of CDT's built-in indexers. The id needs to identify your extension uniquely (so you can make it something like myproject.MyIndexer.)

How to stop resharper removing empty switch default case

Assume I've got the following code:
public static void PrintFoo(int i)
{
switch (i)
{
case 0:
Console.WriteLine("bar!");
break;
case 1:
Console.WriteLine("baz!");
break;
default:
// do nothing
break;
}
}
I want the "default" switch case there with the comment as it shows I'm deliberately not processing any values other than 0 and 1. If I leave out the default case, it's not clear if I meant to do nothing, or just forgot. Indeed, if I delete the default case, I get errors from "IDE0010 Populate switch" showing up in the errors window.
By default Resharper considers this an error, so I have turned off that inspection (Resharper options -> Inspection Severity -> C# -> Redundancies in Code -> Redundant empty switch section).
The problem I have is that when I run code cleanup, it deletes the default case, including the comment. In general I still want code cleanup to fix all the other redundancies in the file, so turning off "Remove code redundancies" in the code cleanup profile isn't an option. Is there a way to get it not to remove the default case in the switch statement?
EDIT: It seems that the default case is only removed if, in the code cleanup window, I select to "Remove code redundancies" and any child of "Code style". If I deselect all the code style items, the default case is not removed, or if I deselect the code redundancies it is not deleted either. Looks like I might have to raise this as a bug with Resharper.
So I reported this to jetbrains who looked into the problem. It turned out that the setting for "Redundant empty switch section" was stored in the "Team shared" settings layer and the code cleanup was pulling the setting from the wrong layer.
The solution was to go to Resharper -> Manage options -> double click on "this computer" and change the setting there.
Thanks to Alexander Kurakin for solving the issue.

Sitecore IsPageEditor and IsExperienceEditor

We are currently writing a module for Sitecore and have ran into a problem.
We have a pipeline in which we do the following check:
if (Sitecore.Context.PageMode.IsExperienceEditor)
{
return;
}
The problem is that one of our clients are running and older version of Sitecore (8.0 update 5) where the property IsExperienceEditor does not exist yet. See Sitecore release notes for next update where it is introduced.
To quickly fix the error we used the older deprecated property which is this:
if (Sitecore.Context.PageMode.IsPageEditor)
{
return;
}
Now the question is, is there any way in which we can test for the Sitecore version so we can have backwards compatibility in the module?
You can use the code which is executed in Sitecore in background of both properties mentioned by you:
if (Sitecore.Context.Site.DisplayMode == Sitecore.Sites.DisplayMode.Edit)
{
return;
}
I know that using Sitecore.Context.PageMode.IsExperienceEditor (or Sitecore.Context.PageMode.IsPageEditor) is more elegant, but in a situation when you need to support both old and new Sitecore versions, that's sounds like a good option.
The deprecated property of IsPageEditor is still present specifically for the purpose of backward compatibility. IsExperienceEditor is just a renamed property that does the same thing that IsPageEditor does.
However you can check for the existence of a property like this:
public static bool HasProperty(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName) != null;
}
Another option is to make two different versions of the module, if the implementation becomes significantly different for the different versions of Sitecore.

Switch autocompletion for enum in Visual Assist or Vanilla Visual Studio

Is there anyway to have a switch expand with all the values of an enum using Visual Assist or Vanilla Visual Studio?. The included snipsets just insert a basic switch with just a default entry. I'm using last visual studio 2013 update.
VS 2013 can build a switch statement for an enum using a default snippet. (Note: use a default VS snippet, not a VA Snippet.)
As you type "swit", accept the suggested code snippet from VS. Replace "switch_on" with your enum and press Enter. Your statement will expand. (You need two Enters if one selected your enum from a listbox.)
After the switch has initially been generated, if you add new enum values, then you can use the Add Missing Case Statements command in Visual Assist to populate the switch statement with the new values (shift+alt+q, m).
In Visual Studio 2015, the auto-completion of switch cases is defeated if the discriminator is declared with const as in this example:
auto const enu = static_cast<MediaType>(discriminant);
Remove const, and voila--it works again!
(Then you can put the const back in, if you wish.)

What is the alternative to getActionView() before API level11 in android?

getActionView() for action bar was introduced in API 11, If I want backward compatibility what is the alternative for getActionView() ?
e.g.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_menu, menu);
final MenuItem item = menu.findItem(R.id.menuitem);
item.getActionView() //Works from API level 11
return true;
}
You can use MenuItemCompat.getActionView(MenuItem menuItem) from the support library to get the action view on pre 11 API.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
...
return super.onCreateOptionsMenu(menu);
}
See http://developer.android.com/guide/topics/ui/actionbar.html for details
For backwards compatibility you can use either ActionBarCompat or ActionBarScherlock. In both cases you can use the method getActionView(). You have to be sure that the import, in the first case is from the compatibility library ( android.support.v4.view.MenuItemCompat). If
you use ActionBarSherlock you have to import com.actionbarsherlock.view.MenuItem. Then you should be ok using item.getActionView().
Old
Since August 2013, and I pray people down-voting to take a look. You have to be sure that the OP's question date import, Android introduced in the first case is from the compatibility library ( ActionBarCompactandroid.support.v4.view.MenuItemCompat). Even though If you use ActionBarSherlock is still a valid choice, an option is moving towards ActionBarCompact.
So another option is to use it, and of course, all the importsyou have to came from the support library, e.g. android.supportimport com.v4actionbarsherlock.view.MenuItemCompatMenuItem. Then you should be ok using item.getActionView().