I am using one base dll named as KSWeb.dll. It has main namespace is KSWeb.This dll has main Base class Under KSWeb.KSS.Base.
Now I have added this as reference in of one of the class library project. I want to give namespace to this class library also KSWeb.I have added a new class named as NewCK which inherits from KSWeb.KSS.Base.
When I am doing coding means KSWEB and then process dot(.) then it can found KSS and then found Base also.
My problem is when I build this class library then it gives me error like KSWEB.KSS not found and then I press alter + tab KSWEB.KSS not found.
Why is it so? please refere below code.
Now in this class library project I have create a class like
namespace KSWeb
{
partial class NewCK : KSWeb.KSS.Base
{
}
}
for my point of view it is compulsory to use KSWEB namespace in class library
Please suggest in c#.net or in vb.net.
//Use a namespace alias
using Other = KSWeb.KSS;
namespace KSWeb
{
partial class NewCK : Base
{
//You can now use Other as below
Other.Method();
}
}
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 a COM visible class (marked with [ComVisible(true)] and registered with RegAsm) in C# that I want to unit-test.
My assembly is an unsigned/weakly named dll (and it has to remain this). The class that I want to test has a private member that is not COM visible.
My problem is when I want to instantiate this class in a test it fails with this message:
Test method %MyTestMethod% threw exception
System.EnterpriesServices.RegistreationException: The assembly
%MyAssembly% does not have a strong name.
So how can I use the unit-test framework on an unsigned/weakly named dll?
using System.EnterpriseServices;
using System.Runtime.InteropServices;
[ComVisible( true )]
public class MyComClass: ServicedComponent, IMyClass
{
//SomeAttribute
private MyNonComClass nonCom;
public MyComClass( MyNonComClass _nonCom )
{
this.nonCom = _nonCom;
}
}
As Hans Passant suggested when I removed the ServicedComponent class from inheritance, the unit-tests are running as supposed.
I have created sample application as layers.
I have a layer which is under DataAccess namespace
using System;
using System.Data;
using DataAccess.DataAccessLayer; //This line is showing error
namespace AdapterClass
{
public class SchemaAdapter
{
}
}
Inside a file, intellisense of current namespace only available.
For ex:
In above example, intellisense of AdapterClass only available.
Be careful with your terms. A namespace is different than an assembly. If your data access class (and namespace) is in another project (say class library) then it is in a different assembly that needs to be referenced. To do that, you can:
Right click the project you shown the code above and right click References => Add Reference => Solution Tab => Click that project
When I'm developing a ConsoleApp there is no problem to use classes in my Main that I've created in separated files (Project Menu --> Add Class). But later, when I try to do it in WPF that class is not recognized. I have made sure that the namespace it's the same both in my "MainWindow.xaml.cs" and my Class Canal.cs. When I define that same class but inside MainWindow.xaml.cs everything works fine, but due to the extension of the code I prefer separate it.
MainWindow.xaml.cs:
//using
namespace Tcomp
{
public partial class MainWindow : Window
{
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ //Stuff but I can't use class created outside of MainWindow.xaml.cs
}
}
}
Canal.cs
//using
namespace TComp
{
public class Canal
{ //some propreties here
}
}
Thanks.
Create the class inside a library project not on a console app. And on your WPF project, add a project reference to the library project you have created.
#mcxiand already answered your question. I would like to add another option: you can use a
public partial class MainWindow : Window
and add it to as many files as you want containing your code, thus there will be no need to create additional class library. The key word here is partial, which allows the code encapsulated in this class to spread over multiple files (.cs).
You must either instantiate the Canal class:
var myClass = new Canal();
and then you can use the properties from it. Make myClass a private member of your MainWindow.xaml.cs and you can access it anytime. Or the second way, make Canal class static and then you can access it from everywhere. Hope this helps.
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.