I am using an asp.net 3.5 web solution with js which contains 2 projects ProjectA and ProjectB. ProjectA has a reference to ProjectB. Now I would like to use a class that sits in ProjectA from somewhere in ProjectB? Vs.net wont let me refer to ProjectA now because of a circular reference? Do I have to refactor of is there a way?
Move common code to Project C and add reference to it from Projects A & B.
Or merge both projects into one.
I encountered this type of error before because I was Declaring a variable Globally inside a class referencing ProjectA. I resolve it by instead of declaring variable Globally, I declared it inside a Sub/Function
Project B
Public Sub ReferenceProjectA()
Dim objProjectA As New ProjectA
objProjectA.ProjectAFunction("")
End Sub
Related
I have a solution with 3 projects. In project1 when I refer to a class in the local project CreateInstance works fine.
Dim oo As New Object = Assembly.GetExecutingAssembly().CreateInstance("TestClass", True)
oo.TestSub()
But when it's in one of other projects it returns "Nothing".
Dim oo As New Object = Assembly.GetExecutingAssembly().CreateInstance("Project2.Business.TestClass", True)
oo.TestSub()
Any ideas?
Thanks!
Types are stored per assembly (usually a project). GetExecutingAssembly always refers to the assembly that the currently executing code is a part of. If you want to create an instance of a type froma different assembly you will need a reference to THAT assembly, or call GetExecutingAssembly from inside that assembly.
You can use 'Assembly.Load' to get a reference to an assembly (even if it is already loaded) and call '.CreateInstance' on that.
I'm used to Ninject, and for a specific project I'm asked to learn Unity.
There is one thing i can't find information on how to do.
In Ninject I can state:
Bind<IWarrior>().To<Samurai>().Named("Samurai");
Bind<IWarrior>().To<Ninja>().Named("Ninja");
Bind<IWeapon>().To<Katana>().WhenInjectedInto(typeof(Samurai));
Bind<IWeapon>().To<Shuriken>().WhenInjectedInto(typeof(Ninja));
And then when one asks for the warrior named samurai, the samurai comes with kanana and the ninja comes with shurikens. As it should.
I don't want to reference the container in the warriors to get the appropriate weapon, and don't want to contaminate the model with attributes (is in another assembly that doesn't have reference to ninject or unity)
PD: I'm looking for a code way, not via xml config.
This should work with Unity:
Container
.Register<IWeapon, Katana>("Katana")
.Register<IWeapon, Shuriken>("Shuriken")
.Register<IWarrior, Samurai>("Samurai", new InjectionConstructor(new ResolvedParameter<IWeapon>("Katana"))
.Register<IWarrior, Ninja>("Ninja", new InjectionConstructor(new ResolvedParameter<IWeapon>("Shuriken")));
Test:
var samurai = Container.Resolve<IWarrior>("Samurai");
Assert.IsTrue(samurai is Samurai);
Assert.IsTrue(samurai.Weapon is Katana);
var ninja = Container.Resolve<IWarrior>("Ninja");
Assert.IsTrue(ninja is Ninja);
Assert.IsTrue(ninja.Weapon is Shuriken);
i have an microsoft .office.interop.excel(dll) located at an directory d:\abc. now i do not want to add them as an web reference in my projet and call them
rather call the dll dynamically from my code behind(.cs)
is ther any way we can do dynmically
anyhelp would be great
thank you
Yes, but you will need to use reflection because if you don't add the assembly as reference it won't be known at compile time. Take a look at LoadFrom method.
var assembly = Assembly.LoadFrom(#"d:\abc\microsoft.office.interop.excel.dll");
var someType = assembly.GetType("Namespace.Type");
var instance = Activator.CreateInstance(type);
someType.InvokeMember(... // the reflection pain goes on
Take a look in Assembly.Load() method.
I want to discourage you from doing that. It can definitely be done if read the dll into a byte[] and call AppDomain.CurrentDomain.Load(byte[]). However you will find that you can only work with the types of that assembly through reflection. Otherwise your code behind file will not compile. So if at all possible you should add a reference (not a web reference) to the dll.
In solution explorer of asp.net application we are adding something in References section
for eg:in our project there are sample.Dal,sample.exeption ,system.core etc
What is actually References means,,,can we add by 'using' statement
Using is used for namespace resolution. For example:
using System.Data;
lets you access the DataSet class without typing in the fully qualified name; System.Data.DataSet.
This doesn't however tell the compiler what assembly (DLL) the DataSet class lies in. So you need to tell it. So you refer to System.Data.dll by adding it to the references section in solution explorer.
A reference references assemblies required for the current project.
Where using statements reference namespaces for the current file.
And yes, a referenced namespace must exist in one of the referenced assemblies.
Yes, once you reference another project or assembly, it's namespaces and types are available for use in the project that references them (you must reference the project or assembly before you can use the types within it).
You can either use using declarations or fully-qualified type declarations to access the types, as in:
// Example1: The using keyword.
using System;
void Example1()
{
Int32 myExample;
// etc.
}
// Example2: Fully-qualified type.
void Example2()
{
System.Int32 myExample;
// etc.
}
Note: I have used C# here.
I have a class Customer in app_code folder in asp.net web site, how can I create an instance using reflection, for example using Activator.CreateInstance(assemblyName, typeName)? Because the app_code is dynamically compiled, I don't know the assembly in design time?
Thanks
Fred
The question is should be how get a full name of type in design time, I want to put it in web.config. I have ConfigSection type, it is in app_code folder, I need to declare it in configSection. Thanks
You should be able to use "App_Code" or "__Code" as the assembly name in the web.config
I think you can use Assembly.GetExecutingAssembly() to get a reference to the current assembly.
I have solved a similar problem in this way:
Type[] appCodeTypes = System.Reflection.Assembly.Load("App_Code").GetTypes();
You can also use GetType().Assembly if you know that it will be the same assembly as your currently executing code.