Calling an assembly
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Calling an assembly

Hybrid View

  1. #1
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    962

    Smile Calling an assembly

    [CODE]
    Code:
    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
    Code:
                ClassLibraryB.Class1 objB = new ClassLibraryB.Class1();
                objB.Add(4, 16);
                objB.Add2();
                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

    Code:
                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?

    Thanks

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

    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

This is a CodeGuru survey question.


Featured


HTML5 Development Center