I created a solution called Foo.
Added a class library called Foo.Common
Added a console app to call the library code from called ConsoleApp.
I referenced the Foo.Common from ConsoleApp and typed :
using Foo.Common;
public class Program
{
CommonClass c = new CommonClass();
static void Main(string[] args)
{
}
}
and get this back :
Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1
Why am i getting this?
what s going on?
Make sure that
The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),
the file contains a using directive for the namespace in which CommonClass is declared, and
CommonClass is declared as public.
So your files should look like this:
CommonClass.cs in Foo.Common project:
namespace Foo.Common
{
public class CommonClass
{
public CommonClass()
{
}
}
}
Program.cs in ConsoleApp project:
using Foo.Common;
namespace ConsoleApp
{
public class Program
{
public static void Main()
{
CommonClass x = new CommonClass();
}
}
}
Ensure that under your project settings, the target framework is set as .NET Framework 4 and not .NET Framework 4 Client Profile. I got this same behavior when it was set to Client Profile and it was fixes as soon as I set it to just the regular .NET Framework 4.
Right Click on the new console app solution/project and Add Reference and add the project that contains the Foo namespace
Did you add a reference to the library? Look under "References" in the console project. If its not there, you need to add it.
I posted this as a comment, but I want to expand on it here. What's probably happening is it's seeing using as a statement and not a keyword. It appears you have something like the following:
using System;
namespace TestNamespace
{
using Foo.Common;
public Class { }
}
Try
using System;
using Foo.Common;
namespace TestNamespace
{
public Class { }
}
Instead.
It looks like Foo Bar got this error because his project's target framework was set to the client profile.
Just thought I'd add one more 'solution' -- I created a library that targeted the 4.5 framework. My older project was tarting the 4 framework. I got this error.
Changing the older project to 4.5 made it work.
Related
We are in the beginning stages of converting a c# Winforms App from .NET Framework to .NET 6. We can get the project to build and run in .NET 6, but when it comes to a dynamically loaded assembly, we are having issues. We can get the assembly to load but attempting to access the custom class within it returns a null. I recreated this scenario in two smaller projects as an example.
Solution 1/Project 1 - The code for the assembly to be loaded into the main application. This is a class library that creates the TestAssembly.dll
namespace Custom.TestAssembly
{
public class TestClass : CallingModule
{
public override object GetValue()
{
return "Hello World";
}
}
}
Solution 2/Project 1 - This is a project and class within the main application's solution. This is a class library that creates the Custom.TestAssembly.dll
namespace Custom.TestAssembly
{
public class CallingModule
{
public virtual object? GetValue()
{
return null;
}
}
}
Solution 2/Project 2 - A button has been placed on a form. When it is clicked, the assembly should be loaded, which it is. However, attempting to extract the class from the assembly always returns a NULL.
Form1.cs
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;
namespace TestCallingApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Assembly dynamicAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(#"C:\LocationOf\TestAssembly.dll");
Module customizationModule = dynamicAssembly.GetModule("TestAssembly.dll");
Type customClientModule = customizationModule.GetType("Custom.TestAssembly.TestClass"); //THIS RETURNS A NULL??
}
}
}
Just trying to understand what I am missing. Any thoughts? Or a better way to load runtime assemblies and access classes within them in .NET 6?
Did you reference Solution 2/Project 1 ?
Since they have the same assembly name Custom.TestAssembly, the runtime will not load it again if already loaded in memory.
You can, however, load it under a different AssemblyLoadContext, there's an example on MSDN as well.
Also, you may want to take a look at DotNetCorePlugins, which takes care of assembly loading, reloading, isolation, shared type, and dependency resolving.
I have 2 solutions under VS2019: RLibrary (contains C++ project) and VTApp (contains C# project).
Solution RLibrary has two projects: RLib C++ .dll and SwigW (wrapper project).
SwigW generated the following files: RLibCSHARP_wrap.cxx, RLib.cs, RLib.i and RLibPINVOKE.cs.
In the RLibPINVOKE.cs I have lines like:
class RLibPINVOKE {
//...
static RLibPINVOKE() {
}
[global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Create")]
public static extern global::System.IntPtr new_Create();
[global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Request")]
public static extern global::System.IntPtr new_Request();
// ...
}
So, after building RLibrary solution I will have two .dll (also .lib) files: RLib.dll and SwigW.dll.
I want to use new_Create(), new_Request() functions from the VTApp.
VTApp has two projects as well: UI and Core. UI has a reference to Core. I added RLibPINVOKE.cs file to the Core project by just Right Click->Add->Existing item and tried to use above mentioned functions from the UI project within namespace UI, i.e.,
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using Core;
using Newtonsoft.Json;
namespace UI {
public partial class MainWindow {
private void Execute_Req(object sender, EREventArgs e)
{
//...
IntPtr p = new_Create();
}
}
}
Unfortunately, I am getting CS01013: The name 'new_Create' does not exist in the current context. error.
In my opinion, even without adding SwigW.dll to the Core project I shouldn't have had this kind of compile time error. So, what is the problem here? How can I add SwigW.dll into the Core project properly if I need to add it? Also, should I add RLib.dll to the Core as well?
Try RLibPINVOKE.new_Create(). The method is inside a class RLibPINVOKE
class RLibPINVOKE {
//...
You'll perhaps need a using, but the Visual Studio should be able to suggest it to you on the RLibPINVOKE.
I have created a NuGet package on .NetCore class library and accessing it from another .net core class library. I can't call my public and static method from a public and static class even tho the class name is detected.
One point to make is that the Package works on a Console App properly, but I am having trouble in accessing funcions in a class library.
To create a package I went to property->Package->Create Package on build and also nuget.exe method. Both lead to the same thing
Both the package and the new project are .net core 3.1 class libraries
Can someone please help me out
NuGet package code
using System;
using System.Collections.Generic;
using System.Text;
namespace TrailCorePackage.helper
{
public static class Utility
{
public static void print(string value)
{
Console.WriteLine(value);
Console.ReadKey();
}
}
}
Code while calling the function in another class library
using System;
using TrailCorePackage.helper;
namespace ClassLibrary5
{
public class Class1
{
Utility.print("value");
}
}
Error
Severity Code Description Project File Line Suppression State
Error IDE1007 The name 'Utility.print' does not exist in the current context. ClassLibrary5 C:\Users\---\ClassLibrary5\Class1.cs Active
Since I can't comment yet, I'll add it like answer. Have you added a reference to your package into your console app?
UPD.
I've just noticed that you are calling function inside the class body. It won't work. Something like this will probably work.
using System;
using TrailCorePackage.helper;
namespace ClassLibrary5
{
public class Class1
{
public void AnyMethod()
{
Utility.print("value");
}
}
}
I'm having difficulty getting my settings out of App.config. The research I've done so far says that I should be able to use ConfigurationManager.AppSettings (which is part of System.Configuration).
However, I can't get it to compile. And of course it doesn't like the older method (ConfigurationSettings.AppSettings) either as its obsolete.
What have I missed here?
Is it my project's Target Framework (currently set to ".Net Framework 4 Client Profile
")?
And example of my code follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace myNameSpace
{
class Program
{
private void LoadAppConfig()
{
string mySetting;
//This won't compile
mySetting =
System.Configuration.ConfigurationManager
.AppSettings["mySettingName"];
//This compiles but of course is obsolete, and I get that warning.
mySetting =
System.Configuration.ConfigurationSettings
.AppSettings["mySettingName"];
}
static void Main(string[] args)
{
// stuff happens
}
}
}
You need to add a reference to System.Configuration
I'm using C#.net 4.0 with Visual Studio 2010. I am getting the error
Error 10 The type or namespace name 'IRange' could not be found (are
you missing a using directive or an assembly
reference?) C:\git\emtexporter\EMTExporter.IRepository\IRangeRepository.cs 11 27 EMTExporter.IRepository
IRange is an interface in the project EMTExporter.IEntities and the project IEntities builds successfully. IRange.cs has the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wiggle.EMTExporter.IEntities;
namespace Wiggle.CategoryXMLExporter.IEntities
{
interface IRange
{
long ID { get; }
Dictionary<ILanguage, string> rangeNames { get; set; }
}
}
The problem occurs in IRangeRepository.cs which has the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wiggle.EMTExporter.IEntities;
namespace CategoryXMLExporter.IRepository
{
interface IRangeRepository
{
Dictionary<string,IList<IRange>> getAllRanges();
}
}
I am referencing EMTExporter.IEntities in the IRepository project.
I have no idea what could be going wrong!
edit: the issue was that the project was changed from CategoryXMLExporter to EMTExporter, but I hadn't updated the Assembly name and Default namespace of the project. Updated that, made the interface public and now it works!
Your interface is not public try the following
public interface IRange
The default accessibility level top level classes and interfaces is internal, not public, so if these are in different projects, it won't be visible.
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
namespace Wiggle.CategoryXMLExporter.IEntities
{
public interface IRange
{
long ID { get; }
Dictionary<ILanguage, string> rangeNames { get; set; }
}
}
You need to add the namespace Wiggle.CategoryXMLExporter.IEntities to your using clauses, because IRange is defined there:
using Wiggle.CategoryXMLExporter.IEntities;
Also, if it's in a different assembly, you need to make it public.
IRange is in the namespace Wiggle.CategoryXMLExporter.IEntities so you will need to reference that. Also make the Interface public
In addition to what has been mentioned above, sometimes you may need to ensure the referenced library is configured as being built under the selected active configuration and platform.
In Visual Studio 2012 or 2013, right-click on the Solution and chose "Configuration Manager".
For the selected "Active Solution Configuration" (e.g. "Debug" or "Release") and "Active Solution Platform" (e.g. "Any CPU", "x64", "x86", or "ARM") ensure the project that contains your interfaces and any of its dependencies have "Build" checked.