1

Here is a sample piece of code where im trying to send a var type variable to another function as a reference parameter.

 Type type = Type.GetType(te1.GetValue("class1"));
        MethodInfo method = type.GetMethod("fnc1");
        var obj1 = Activator.CreateInstance(type);

I cant use (class1)Activator.CreateInstance(type) since the variable class1 will be decided at runtime. That's why im using reflection here.

   method.Invoke(obj1, new object[] { });     
    class2 cls2= new class2 ();
    cls2.fnc2(ref obj1);//getting error  'cannot convert object to class type'

Im getting error as the fnc2 function receive parameter as a type of class1.

 public string fnc2(ref class1 obj3)
    {
    }

How i can handle this? I need to get the parameter as a type of class1 using reflection. So i used the type var. I cant use dynamic since im using framework 3.5.

My requirement is create a object of class1>> execute a function of class1>> retain the object and pass that object to another function of class2 and process there.Also the name class1 and class2 will be decided dynamically and cannot be hard coded.

  • try this line var obj1 = (class1)Activator.CreateInstance(type); – Zohaib Aslam Sep 05 '14 at 05:29
  • you're creating an instance of 'class1' while you're passing it to a method which expects to receive refrence of 'class2'. this is not possible bro – Zohaib Aslam Sep 05 '14 at 05:35
  • what is `te1`? (I assume you are using reflection here). And later in the last snippet of code you have access to type `class1`, then why are you reflecting? – deostroll Sep 05 '14 at 05:56
  • The area where the function fnc2 invoke and the define the function needs to be redesigned. I dont know how to send parameter and later access it in another function. My requirement is create a object of class1>> execute a function of class1>> retain the object and pass that object to another function of class2 and process there. –  Sep 05 '14 at 06:13

2 Answers2

3

var is not a type. Rather it tells the compiler to "automatically determine the type" (and saves typing). Hover the mouse over the obj1 in the next line and Visual Studio will pop up a handy little box saying what the actual type is resolved to - as the error message indicates this is, well, Object.

(Some of the following may be outdated after updates to the question, but the same principles apply - the expression must be cast as appropriate type, which must be determined at compile-time, before it can be used as anything besides Object.)

A "solution" is just to cast the result of the Activator result (because we know this to be the case):

var obj1 = (class1)Activator.CreateInstance(type);
// same as: class1 obj1 = (class1)..

However, since we know this then we could avoid all that and use the following, which is boring:

var obj1 = new class1();
// same as: class1 obj1 = new class1();

So, now to the Y problem: this cannot be done for an arbitrary type and a cast is required. The types known by the compiler which can used as such in code must be known at compile-time; they cannot be directly resolved from run-time values.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

If you're sure the type of obj1 is of the type your fnc2 expects then you could use reflection like so:

Type type = Type.GetType(te1.GetValue("class1"));
MethodInfo method = type.GetMethod("fnc1");
object obj1 = Activator.CreateInstance(type);

method.Invoke(obj1, new object[] {});
class2 cls2 = new class2();

cls2.GetType().GetMethod("fnc2").Invoke(cls2, new[] {obj1});

This retrieves the method called fnc2 (assuming there's only one with that name, no overloads) and invokes it with obj1. If there are overloads, you will need this GetMethod variant.

On a sidenote, using var in this case is really not that useful. For clarity, I think you should just use object.

Vincent
  • 1,459
  • 15
  • 37