C# Change using namespace reference dll path dynamically IN .NET LIBRARY PROJECT - c#

I have a class library .net project(no config,no exe file) and I have something like this;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Web.UI.WebControls;
using eBAControls; *<-- I want this dll to be loaded from a path in runtime.*
I have to make copyLocal false for future reasons thus i need to be able to refer the dll in runtime. Other 3rd party dlls which are in same folder with this current dll do not cause any problems but eBAControls dll is not in the same folder with this current dll thus it gives the error: Could not load file or assembly 'eBAControls...
I can not also use reflection for that for some coding and performance reasons thus it needs to be added to references...
Shortly what i want is
using eBAControls = #"somepath"; <- that is set dynamically for ex while one of my methods are being executed etc...

You can load the assembly from wherever you like if you place an event handler on AppDomain.AssemblyResolve. See Resolving Assembly Loads
Here's a blog post that has the essentials. The key is timing. You have to attach the event before the first method referencing the assembly is JITted. https://blogs.msdn.microsoft.com/dbrowne/2014/06/25/how-to-load-an-assembly-in-a-ssis-script-task-that-isnt-in-the-gac/

Related

Is it possible to display package version numbers next to the usings?

Suppose that at the top of my *.cs file, I am including some dependencies:
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
Is it possible to also display version numbers here as shown below?
using System; //1.0.1
using System.Collections.Generic; //22.0.1
using System.IdentityModel.Tokens.Jwt; //3.56.2
etc
No, you can't. This is because C# and .NET is not like Java, TypeScript and other languages where the concept of namespaces, packages and libraries are combined - in .NET they're entirely separate: multiple packages can include the same library DLL file, and multiple library DLLs can contribute towards shared and different namespaces.
For example, the System namespace has its members in mscorlib.dll, System.dll, System.Core.dll, and others. None of these assemblies belong to any particular package as they're part of the BCL in your CLR implementation. Furthermore, anyone is free to declare their own types in namespace System.
With respect to "packages": the CLR is completely unaware of them: they're a design-time concern; instead all it cares about is what DLL files to link. You don't have to use NuGet packages at all (.NET developers survived just fine for the first 13 years without a built-in package manager).
Note that NuGet allows you to have multiple DLL files a single package, and the package author does not need to "own" (legally or otherwise) the DLLs contained within either, which means multiple NuGet packages can share the same assembly DLL, and those DLLs could share namespaces.
I take issue with these words of yours:
I am including some dependencies:
Remember, C# is not TypeScript - despite syntactic similarities (like C compared to C++) they're very different - it isn't appropriate to refer to namespace imports as "including dependencies" - it's just syntax - whereas in TypeScript the statement import { Foo } from './bar' is more than just syntax as it will invoke the runtime platform's package/module import system, whereas using in C# is an entirely inert statement that simply saves you from typing global::The.Full.Namespace.Name every time you use an identifier.

adding a dll to c# project and using the dll functions

enter image description hereI am trying to import a dll in my c# console project.
I add the dll in the reference of the project. Right Click->add reference->browse.
The DLL is placed in my project folder as well as in program data folder in c drive.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using mcp2210;
the last line "using mcp2210;" gives an error
Error 1 The type or namespace name 'mcp2210' could not be found (are
you missing a using directive or an assembly
reference?) C:\Users\testuser\AppData\Local\Temporary
Projects\ConsoleApplication1\Program.cs
Do I need to use [DllImport("mcp2210.dll")] in my project?
How can I access the functions of this dll?
Please see the images below
edit
Hi I was able to add the dll in my project using the namespace from the object browser. However when I try to run it in debug mode I get badImageFormatException was handled popup.
Could not load file or assembly 'mcp2210_dll_m_dotnetv2_x86, Version=1.0.5980.19136, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Why is this happening?
Namespace has nothing to do with a DLL's name. Just because the DLL is named mcp2210 doesn't mean it contains the namespace mcp2210.
After you've added the reference, open Visual Studio's object browser. Find the DLL in the treeview on the left and expand it to discover the namespaces and classes contained in it.
Once you find it, use one of its namespaces in your using statement and you're good to go.
If you cannot browse to your DLL, it is probably the wrong type of DLL or the wrong framework version.
Make sure if it is a managed dll. If it is build using unmanaged than place this dll in bin folder manually.
just copy paste in bin
Firstly, make sure both the dll project and your separate project have the same target platform (32 bit, 64 bit, or any cpu by going to the project's properties).
Secondly, make sure you have added the dll as a reference to your project. In addition to this, place the file in your project's 'bin > debug' folder.
The path should look like this (..\Your Project\bin\Debug).
If all of that fails, double check that the Target Framework matches in each project.

How do I designate the location of a library in order for a "using" statement to work (C#)

I'm used C# -some- but not recently.
I've got this header from some code, and it calls a custom non-system library, (Dynastream.Fit, at the bottom), but I need to know how to tell the program where it resides so it can compile. Pretty sure the library is in the SDK I downloaded, just need to find it. This is the FitSDK from ANT+.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Dynastream.Fit;
You need to add the dll of Dynastream.Fit to your project Reference folder by right clicking the reference folder -> add reference -> Browse -> choose the dll; unless you have already installed the assembly in GAC.

C# using System.Xaml missing assembly reference

I'm writing a C# Console Application that is targeted towards .Net 4.5. I want to use Xaml Services to save and read a List data structure to file. I'm using VS 2013 Pro.
The .net doc's say Xaml has been in .NET since 4.0(?) I have my projected targeted to 4.5, but even with 4.0, 4.5.1, 4.6, and 4.6.1... same missing assembly reference. I'm doing a
using System;
using System.Collections.Generic;
using System.IO;
using System.Xaml; // <-- this is getting the assembly error
using System.Xml;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
But that is where the missing reference is getting the error...
I've used it before in Win Forms. It is that maybe it's excluded for console applications? Or am I missing something that Xaml Class depends on before using that assembly?
Could it be that you did not add a project reference to the assembly "System.Xaml.dll"? The Xaml functionality is not contained in the assemblies which are included in a new VS console project by default. (Right-Click on the "References" entry of the project, then select "Add References", then browse to "Framework" and look for System.Xaml, which refers to the dll of that name).
A namespace however does not necessarily correspond uniquely to an assembly, so you might require even more assemblies. If you know which types you need, you can browse the MSDN documentation for looking which assembly might still be required.

namespace or class could not be found (ASP.NET WebSite "project")

I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
I have one masterpage that have this code :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
Now in App_code I have one class lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
Just edited with news details on the code itself. No namespace at all everywhere.
Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file’s build action was set to “Content” not “Compile” (in the properties window.
For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
There is a case that this happens, when you place a web site inside another (ie in a subfolder).
In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.
include the namespace under which lists calss is defined
or
define both the master page and lists class under the same namespace
Finally I understood quite lately that it was a website and not a web application I had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.
Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
The Way I See It: Where is ASP.NET 3.5 on IIS?
In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.
is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace
Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
This worked for me.
I had this problem myself
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library

Categories

Resources