what happens not using c# namespace - c#

Hi will write a dll in c# with not without namespace.
I'll write a class then converting it to dll, i.e c# default library type is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace temp
{
public class c1
{
public static string f1(string deger) {
return deger.ToUpper() + " 333 ";
}
}
}
now I will write this code so :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class c1
{
public static string f1(string deger) {
return deger.ToUpper() + " 333 ";
}
}
is that twou codes are same on runtime

Then this class will be at .NET unnamed global namespace(not in assembly root namespace). And it can be accessed from anywhere as "global::c1" or just "c1"

By convert this code to dll I think you mean to compile as a library. The namespace (or lack of) has no special effect in this regard.
If you want to reference this class without referencing the dll you will need to load the dll dynamically.
Example:
Assembly a = Assembly.LoadFrom("mylibrary.dll");
Your next problem is to reflect and use the class. Usually you would have a library that defines some interface or interfaces.
InterfaceLibrary.dll Contains interface IC1
PluginLibrary.dll Contains class C1 which implements IC1
Program.exe
Both the Program and the plugin library refer to the interface library. The program can dynamically load PluginLibrary and look for classes that implement IC1.
Here is a more in-depth example of a plugin architecture in C#: https://stackoverflow.com/a/829828/360211

Related

I cant get SevenZipLib library to work (p/invoke)

Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.

cannot create instance of supersocketserver AppServer

I am trying to get the SuperSocketServer app up an running. However, I cannot seem to create an instance of the server. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Protocol;
namespace WinnieServer
{
class Program
{
AppServer W = new AppServer();
static void Main(string[] args)
{
Console.Write("Starting...");
while (true)
{
}
}
}
}
I tried the examples in the source code. However, they all give the same error:
Cannot create an instance of the abstract class or interface 'SuperSocket.SocketBase.AppServer'
Is this the correct way to create an instance of SSS?
take care,
lee
I had this same issue. The download on their home screen gives you the 1.4.4 version. If you look in object explorer it IS abstract. If you go to their downloads and grab the binaries zip, you'll get the 1.5 version that ISN'T abstract.

CS0234 - Cannot Instantiate C# class in ASP.NET

I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!
If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.
You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.
If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.

'namespace used like a type' error when converting XAML to HTML

Coders, I am trying to convert a XAML string to HTML using a library I found here , but I have a problem with creating a new instance of the object that would let me use the library. I already added a reference to the library in my Asp.net project and I would like to use it in a WCF file.
The problem is that whenever I try to instantiate a new object with the new keyword, I get an error that says:
'MarkupConverter' is a 'namespace' but is used like a 'type'.
Here is my code, notice that I am creating a new object just like the example shown in the library link above, please help:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Services;
using System.Net.Mail;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using MarkupConverter;
namespace AspPersonalWebsite
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 //: IService1
{
private string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
private IMarkupConverter markupConverter;
[OperationContract]
public string convertXAMLToHTML(string XAMLtext)
{
string htmlText = "";
markupConverter = new MarkupConverter(); /*PROBLEM IS HERE*/
htmlText = markupConverter.ConvertXamlToHtml(XAMLtext);
return htmlText;
}
}
}
Confusion is arising because the actual type is MarkupConverter.MarkupConverter, the compiler seems to think your new MarkupConverter is an attempt to use a namespace as a type, rather than an attempt to instantiate a type inside your using namespace.
Simply change your problem line to:
markupConverter = new MarkupConverter.MarkupConverter(); /*SOLUTION HERE!*/
..and you should be fine.
In your case, you have a namespace MarkupConverter and a class with the same name (MarkupConverter again).
In the line markupConverter = new MarkupConverter(); /*PROBLEM IS HERE*/ the compiler is unable to tell that you intent to use the class. Since a namespace with the same name is present, the compiler picks it instead, because namespaces are linked with higher priority by the compiler.
You can resolve this by using the complete name of the class:
// supposedly MarkupConverter is the namespace of the MarkupConverter class
markupConverter = new MarkupConverter.MarkupConverter();
An alternative way to providing the fully-qualified name of the class is to use an alias, which takes the form of using {ALIAS} = {Fully qualified name of Type| Namespace}. Note that the {ALIAS} part can be any valid identifier.
The alias you can place either in your usings:
using System.Net.Mail;
using System.ServiceModel.Activation;
using System.Data.SqlClient;
using MarkupConverter;
using MarkupConverter = MarkupConverter.MarkupConverter; // this is aliasing
or after the namespace declaration:
using System.Data.SqlClient;
using MarkupConverter;
namespace AspPersonalWebsite
{
using MarkupConverter = MarkupConverter.MarkupConverter;
....
and you're good to go! At this point, if aliases are present, the line
markupConverter = new MarkupConverter()
will correctly pick the MarkupConverter class, because explicit aliasing has higher priority than the automatic binding done by the compiler.
That is pretty much self explanatory,
MarkupConverter is a namespace ,so shouldn't be used as a class to create an object
Can you show the MarkupConverter class you use please? The error is maybe in its declaration. In Where namespace is it? What is your file structure?
Maybe you have created a MarkupConverter namespace?
You should add a "using MarkupConverter" statement in the usings section. That will import all classes from that namespace.

C#: Referencing a windows shell interface

I'm pretty new to C#, I'm trying to complete a little side project I've been working on that uses a small amount of C# code to assist the development of a Windows Desktop Gadget. Basically, I'm trying to implement the IDesktopGadget interface so that I can use the RunGadget method.
Here's what I got so far from reading information about similar interfaces:
[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}
Unfortunately, I get an error when I try and create an object from it: "Cannot create an instance of the abstract class or interface 'GadgetTask.IDesktopGadget'"
Can someone point me in the right direction and maybe help me understand what I'm doing wrong at the same time?
You actually need an implementation of the DesktopGadget object in order to use the interface. MS provide a standard COM object to do it on Windows 7. You can create an instance by doing something like:
Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
Thanks for the guidance. For a little more straight forward help, this is what worked for me:
IDesktopGadget.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace GadgetActivator
{
[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GadgetActivator
{
class Program
{
static void Main(string[] args)
{
Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
dg.RunGadget(#"C:\Program Files\Windows Sidebar\Gadgets\xxxxxxxxx.Gadget");
}
}
}

Categories