okay, so to start with i have set up the references in the project that i am useing the dll in.
what i am trying to do is access the method "haha" in my utils dll
code for dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
public class kb
{
public class yes {
public void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
}
}
and in the project im trying to access haha in i have just "Utils.kb.yes" but there is no method in that.. all i can do is Utils.kb.yes.equals and Utils.kb.yes.ReferenceEquals.
Since haha() is an instance method, you need to create an instance of the Utils.kb.yes class first:
Utils.kb.yes kb = new Utils.kb.yes();
kb.haha("nextproblem");
Or you also can make the method static:
public class yes {
public static void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
then you can call it like this:
Utils.kb.yes.haha("I am static!");
Your classes do not have a constructor, and besides that, you simply CAN'T do much with a class before instantiating an object out of it. So you should reference your dll, and then create a new object first. From within that object, you can then reference your method(s).
Related
In my current software I make use of serialization and therefore need everything to be marked with the [Serializable] attribute.
Is there an easy way of checking this using my Visual Studio without going through them one at a time, or just waiting for it to crash?
To clarify, I don't need to know how to check if a class is serializable in code. I'm talking about using the IDE.
If you want to use reflection to find classes not marked [Serializable] you can use reflection to get the class types via GetTypes and then find only those not marked with Serializable.
try this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReflectOnSerializableAttr
{
class Program
{
static void Main(string[] args)
{
//use Linq
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && ((t.Attributes & TypeAttributes.Serializable) != TypeAttributes.Serializable)
select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));
Console.ReadKey();
}
}
[Serializable]
public class TestSerializableOne
{
public string SomeFunc() { return "somefunc"; }
}
public class TestForgotSerializable
{
private int _testInt = 200;
}
}
The above program outputs:
Program
TestForgotSerializable
I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs
I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between 'protected' and 'protected internal'?
What is the difference between Public, Private, Protected, and Nothing?
Code is as mentioned below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testanotherlib
{
public class A
{
internal void InternalDisplay()
{
Console.WriteLine("Internal Display Method.");
}
protected void ProtectedDisplay()
{
Console.WriteLine("Protected Display Method.");
}
protected internal void ProtectedInternalDisplay()
{
Console.WriteLine("ProtectedInternal Display Method.");
}
public void PublicDisplay()
{
Console.WriteLine("Public Display Method.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testanotherlib
{
public class B : A
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testanotherlib;
namespace testlib
{
public class C:A
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testlib;
using testanotherlib;
namespace testapp
{
class Program
{
static void Main(string[] args)
{
B objB = new B();
C objC = new C();
}
}
}
I am trying to understand the difference between Internal, Protected and Protected Internal. For that I have created an example using the code above.
In a class library project testanotherlib I have class A & class B. In a class library project testlib I have class C. The program class is in a separate console application. Inside the main method of Program class I have created object for class B (objB) and class C (objC). For objB and and objC only the public method of class A are accessible. I was expected for class B all the methods of class A will be accessible. Kindly help me to understand this. If you need any other information about the project, feel free to ask me.
Regards,
Priyank
The following five accessibility levels can be specified using the access modifiers:
public: Access is not restricted.
protected: Access is limited to the containing class or types derived from the containing class.
Internal: Access is limited to the current assembly.
protected internal: Access is limited to the current assembly or types derived from the containing class.
private: Access is limited to the containing type.
Taken directly from Microsoft's MSDN library.
internal
Only visible in the current and friendly assemblies.
protected
Only visible within classes that inherit A.
protected internal
Visible within classes that inherit A. And also visible within the current and friendly assemblies.
protected methods and members can only be accessed from another Class that derives from the class declaring the procted method.
class A
{
protected void Method() {}
}
class B : A
{
public void Foo()
{
Method(); // works!
}
}
class C
{
public void Foo()
{
Method(); // won't work, obviously
var tmp = new A();
tmp.Method(); // won't work either because its protected
}
}
internal makes the method only visible in the same assembly. For classes in the same assembly the method can be used like it were public. for classes outside of your current assebmly its like private.
Now combining protected and internal makes a method usable in the same assembly for all classes in that assembly. And the protected makes the method usable in all derived classes no matter which assembly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.ComponentModel;
namespace ConsoleApplication1
{
class BreakingChange
{
delegate void SampleDelegate(string x);
public void CandidateAction(string x)
{
Console.WriteLine("Snippet.CandidateAction");
}
public class Derived : BreakingChange
{
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
static void Main()
{
Derived x = new Derived();
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
factory("test");
}
}
}
\Program.cs(32,38): warning CS1707: Delegate 'ConsoleApplication1.BreakingChange.SampleDelegate' bound to 'ConsoleApplication1.BreakingChange.Derived.CandidateAction(object)' instead of 'ConsoleApplication1.BreakingChange.CandidateAction(string)' because of new language rules
\Program.cs(23,25): (Related location)
\Program.cs(16,21): (Related location)
Question:
I know what causes this warning and know the reason behind it. However, I don't know what the
best way to fix it?
1> Redefine the function (i.e.) change the function signature
2> Can we explicitly call BreakingChange.CandidateAction in the following line?
SampleDelegate factory = new SampleDelegate(x.CandidateAction);
Well, there are multiple ways to "fix" this, depending on what you want to, and can, do.
Personally I would add another overload to Derived that took a string, since you're going to have the same issue with non-delegate calls as well.
public class Derived : BreakingChange
{
public new void CandidateAction(string x)
{
base.CandidateAction(x);
}
public void CandidateAction(object o)
{
Console.WriteLine("Derived.CandidateAction");
}
}
Or, since you know you want the base class method, you can cast the reference x:
new SampleDelegate(((BreakingChange)x).CandidateAction)