Using PluralizationService class in dotnetfiddle - c#

I want to do some tests with the class PluralizationService using dotnetfiddle.net but I get an error:
The type of namespace name 'Design' does not exist in the namespace 'System.Data.Entity'
using System;
using System.Data.Entity.Design.PluralizationServices;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
link to the example in dotnetfiddle
I've tried adding a reference to EntityFramework (in the NuGet Packages section) but this has not worked. Does anyone know how to solve this error?

Related

Visual Studio reports a missing method in a Roslyn Analyser but it is definitely there

I'm trying to use this analyser ( I wrote it )
https://www.nuget.org/packages/Weingartner.Json.Migration.Analyzer/
https://github.com/Weingartner/Migrations.Json.Net
I apply it against this source file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Weingartner.Json.Migration;
namespace testjson
{
[DataContract]
[Migratable("")]
public class Class1
{
[DataMember]
public int foo { get; set; }
}
}
and I see the following warning in the output.
1>CSC : warning CS8032: An instance of analyzer
Weingartner.Json.Migration.Roslyn.MigrationHashAnalyzer cannot be created from
C:\Users\phelan\workspace\testjson\packages\Weingartner.Json.Migration.Analyzer.1.0.4\analyzers\dotnet\cs\Weingartner.Json.Migration.Roslyn.dll
: Method 'get_SupportedDiagnostics' in type
'Weingartner.Json.Migration.Roslyn.MigrationHashAnalyzer' from
assembly 'Weingartner.Json.Migration.Roslyn, Version=1.0.6246.21734,
Culture=neutral, PublicKeyToken=null' does not have an
implementation..
which is quite strange because if I crack open the DLL with JustDecompile I see
[DiagnosticAnalyzer("C#", new string[] { })]
public class MigrationHashAnalyzer : DiagnosticAnalyzer
{
<snip>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
return ImmutableArray.Create<DiagnosticDescriptor>(MigrationHashAnalyzer.Rule);
}
}
<snip>
}
which is what I expect because the source for the analyzer compiles. If the method was really missing then it wouldn't compile.
Why would VS report a method missing when it is really there?
Visual Studio is quite particular about the version of the libraries it uses. VS itself uses version 1.1.36 of the System.Collections.Immutable package. Since your analyser uses a different version, the runtime is unable to find the method and assumes it hasn't been implemented.
Reference: https://johnkoerner.com/code-analysis/creating-a-code-analyzer-using-f/#comment-3073977270

PreApplicationStartMethod cannot be resolved

I'm trying to use Unity.Webforms in a web application without success. As per the documentation, I put the following line in the AssemblyInfo.cs to tell the runtime to invoke a method before the application starts:
[assembly: PreApplicationStartMethod(typeof(Unity.WebForms.PreApplicationStart), "PreStart")]
The error I'm getting says:
The method specified by the PreApplicationStartMethodAttribute on assembly ... cannot be resolved
And this is the class holding the method (within App_Start folder):
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System;
using System.Collections.Generic;
using System.Web;
namespace Unity.WebForms
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
Is there anything I'm missing or misunderstanding?
Thanks
Change Unity.Webforms namespace in your custom file with PreApplicationStart, because this namespace already exists.
My solution, that works:
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
namespace TestApp.WebUi
{
public static class PreApplicationStart
{
private static bool _isStarting;
public static void PreStart()
{
if (!_isStarting)
{
_isStarting = true;
DynamicModuleUtility.RegisterModule(typeof(UnityHttpModule));
}
}
}
}
And:
[assembly: PreApplicationStartMethod(typeof(TestApp.WebUi.PreApplicationStart), "PreStart")]

Can´t find assembly from other project

I having trouble finding assembly from a project. I have two Console Applications and one where i make a reference from the other. But then i can´t make the using-statement because it can´t find the project...
EDIT: my Program-class in Lab1PersonId:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab1PersonId
{
class Program
{
static void Main(string[] args)
{
}
}
}
EDIT:
using Lab1PersonId;
namespace Test
{
public class Program
{
static void Main(string[] args)
{
Lab1PersonId lab = new Lab1PersonId(); ***
}
}
}
Error on row ***:
Type name expected, but namespace named found
Your setup seems fine. You have added reference to the Lab1PersonId project inside your Test project, so normally you should be able to use classes that are defined in this assembly.
For example, you should first rebuild the Lab1PersonId project and then you should be able to add reference to some namespace that exists in it:
using Lab1PersonId;

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

C# web project complaining about a class

I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!

Categories

Resources