-2

I have two Windows forms applications that belong to separate solutions. I'm trying to call a method from one using the other. I created a class library from the solution I'm trying to access (Sol1) and referenced it in the project I'm running (Sol2) but I'm having difficulty accessing the method.

I found this solution that suggests doing it this way:

string s = string InvokeStringMethod3 ("TheProject", "TheNamespace", "TheClass", "TheMethod");

public static string InvokeStringMethod3(
                        string assemblyName,
                        string namespaceName,
                        string typeName,
                        string methodName)
{
    Type calledType = Type.GetType(namespaceName + "." + typeName + "," + assemblyName);
    String s = (String)calledType.InvokeMember(
                    methodName,
                    BindingFlags.InvokeMethod | BindingFlags.Public | 
                        BindingFlags.Static,
                    null,
                    null,
                    null);
}

When I run it, I get a NullReferenceException where calledType is null. According to the comments, this will only work on static methods but the method I'd trying to call is public void.

Is there a way to modify this to work for a non-static method? Or should this be done another way? I'm working with C# in Visual Studio 2015, and both projects are .NET.

AxxieD
  • 614
  • 1
  • 7
  • 28
  • 3
    why don't you just instantiate the class this method belongs to? – guijob Jul 06 '16 at 18:56
  • 6
    Do you understand what the difference is between a static method and an instance (non-static) method? You need to understand the difference between the two, not just to answer this question, but to learn C#. – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 18:57
  • @EdPlunkett If I understand it correctly (which I might not, I'm new to C#), static stays the same every time you run the program, instance doesn't. – AxxieD Jul 06 '16 at 19:03
  • You could also just reference the compiled DLL of the other solution... – Camilo Terevinto Jul 06 '16 at 19:05
  • 1
    @AxxieD - no, that's not it. You are thinking of constant or readonly which is something different entirely. – Igor Jul 06 '16 at 19:07
  • 1
    @AxxieD - This is a good write up on the MSDN site that should describe what the differences are with plenty of examples: [Static Classes and Static Class Members](https://msdn.microsoft.com/en-us/library/79b3xss3.aspx) – Igor Jul 06 '16 at 19:09
  • Maybe there is a good reason why this method is private, you know encapsulation. I think you should talk with author of class that contains method that you try to call, maybe there is other better way for you to use that API... – csharpfolk Jul 06 '16 at 19:21
  • Just by fact the method is private what you want is not possible – Paul Swetz Jul 06 '16 at 19:22
  • @PaulSwetz I made a mistake, it's `public void`, not `private`. I corrected it in the question. – AxxieD Jul 06 '16 at 19:24

1 Answers1

2

Create an object of the class and use its function when you have already referenced the solution. No need to to create a method static. Inside solution 2 instantiate your solution 1class.

Sol1.MyClass cls = new Sol1.MyClass();

Then you can use the method.

cls.somemethod();

Static members belong to the class not to the object and Can be used directly by class name.

Juan Salvador Portugal
  • 1,233
  • 4
  • 20
  • 38
Saket Choubey
  • 916
  • 6
  • 11