CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    G day Mayt Land

    Smile Calling an assembly

    namespace ClassLibraryB
        public class Class1
            public int result { get; set; }
            public static int i { get; set; }
            public static int j { get; set; }
            public void Add(int ii, int jj)
                i = ii;
                j = jj;
                result = i + j;
            public void Add2()
                result =  i + j;
    This gets called statically and gives me an answer
                ClassLibraryB.Class1 objB = new ClassLibraryB.Class1();
                objB.Add(4, 16);
                kk = objB.result;
                textBox1.Text += "Addition =" + kk.ToString() + "\r\n";
    However when I try to call the dll using below it fails with since it is not static

                Assembly testAssembly = Assembly.LoadFile(strDLL);
                Type calcType = testAssembly.GetType("ClassLibraryB.Class1");
                object calcInstance = Activator.CreateInstance(calcType);
                PropertyInfo numberPropertyInfo = calcType.GetProperty("i");
                numberPropertyInfo.SetValue(calcInstance, 5, null);
                PropertyInfo numberPropertyInfo2 = calcType.GetProperty("j");
                numberPropertyInfo2.SetValue(calcInstance, 15, null);
                int value2 = (int)numberPropertyInfo.GetValue(calcInstance, null);
                int value3 = (int)numberPropertyInfo2.GetValue(calcInstance, null);
                calcType.InvokeMember("Add2",BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                    null, null, null);
                PropertyInfo numberPropertyInfo3 = calcType.GetProperty("result");
                int value4 = (int)numberPropertyInfo3.GetValue(calcInstance, null);

    it does not like calling Add2 as a non static.
    Non-static method requires a target.

    Any ideas to be able to call the same dll func from both static and dynamic way?


  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Calling an assembly

    A method can't be both static and be an instance method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.