3

i have a requirement to set the property to a client application from the dll referenced.

the technical part explained below.

i have a class instance

public class test { public string Resultobj; public string Result { get { return Resultobj; } set { Resultobj = value; } } test obj = new test(); } 

I am sending this to a method as a parameter which resides in another assembly.

callmethod(test obj ); 

so in the referenced assembly i need to set the values to the instance so that it can be accessed from the application. Can anyone provides suggestion on how to set properties to a class instance that passed as a parameter to a method.

Here I am adding what i tried but its wrong. :-(

public override void callmethod(ref object obj) { Type type = Type.GetType(obj); PropertyInfo property = type.GetProperty("Result"); property.SetValue(type , "somevalue", null); } 

Since the class name instance will be pass at runtime I can not provide the class name as datatype. I'm getting error in the line

 callmethod(test obj ); Argument '1': cannot convert from 'test ' to 'ref object' 
5
  • 1
    property.SetValue(obj, "somevalue"); ? Commented Jul 24, 2013 at 13:45
  • 5
    please clarify its wrong Commented Jul 24, 2013 at 13:46
  • 4
    Wrong in what way? Property is not set? Compiler errors (I can see some issues with the code but assuming they are typos)? Exceptions? You need to be clear on why it's not working as you would expect. Commented Jul 24, 2013 at 13:46
  • Error 1 The best overloaded method match for 'System.Type.GetType(string)' has some invalid arguments Error 2 Argument '1': cannot convert from 'object' to 'string' Commented Jul 24, 2013 at 13:50
  • I think you're working way too hard. The parameter does not need to be ref, and it doesn't need to be object. public void callmethod(test t) { t.Result = "somevalue"; } Commented Jul 24, 2013 at 14:38

3 Answers 3

4

Well first of all you aren't passing the parameter to callmethod correctly, as it expects a reference parameter you need to use the ref keyword e.g.

callmethod(ref (object)obj); 

Then in callmethod itself, change your first line to:

Type type = obj.GetType(); 

Type.GetType requires string representation of the type, not the actual object instance. Then lastly, update the SetValue call to use the object instance, not the type e.g.

property.SetValue(obj, "somevalue"); 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting the below error. Error 1 The best overloaded method match for 'callmethod(ref object)' has some invalid arguments Error 2 Argument '1': cannot convert from 'ref test' to 'ref object'
Sounds like you are passing the Type into callmethod and not the instance. If you write the code as I have written it it should work (assuming the code you have in your question is correct).
No..Im passing instance not Type. What I provided in the question is exactly I have in my program. when i change the line test obj = new test(); to object obj=new test() it works fine.
Oh sorry, my mistake I assumed you had callmethod(ref Test obj) in the signature. object obj = new test() is fine if you don't need to access anything relating to Test outside, otherwise you can just cast it in the call (see update to answer).
0
Type type = Type.GetType(obj); PropertyInfo property = type.GetProperty("Result"); property.SetValue(type, "somevalue", null); 

The first line is wrong. The Type.GetType(...) static method expects a string which tells it which type to load, NOT an instance of the type itself. However, since you have an instance, you could call obj.GetType() which does what you're trying to do.

The third line is also wrong. The first parameter to SetValue needs to be the instance you're wanting to modify, NOT the type. Also, you don't need the third parameter because there is another overload of SetValue that only needs the first two parameters. Try changing this to:

Type type = obj.GetType(); PropertyInfo property =type.GetProperty("Result"); property.SetValue(obj, "somevalue"); 

2 Comments

This is an issue as well, however, the OP isn't even hitting that problem yet. It's the first line with the call to Type.GetType that's failing, it requires a string representation of the type and the OP is passing the object iself.
@James, correct, I hadn't even noticed that (I'd only scanned it by eye since I'd already noticed that the provided code wouldn't compile anyway...)
-1

Uhm ... isn't it simply:

test obj = new test(); callmethod(ref obj); 

I see that you've put test obj = new test(); in the class definition - why?

Also, in C# all class names should use CamelCase. I would rewrite the class definition thus:

public class Test { public string Result { get; set; } public Test(string result) { Result = result; } } 

Usage where callmethod (which should be named CallMethod but I digress) is defined would look something like:

Test testInstance = new Test("Result string"); callmethod(ref testInstance); 

callmethod itself should look something like this:

public override void CallMethod(ref object obj) { Type type = obj.GetType(obj); PropertyInfo property = type.GetProperty("Result"); property.SetValue(obj, "somevalue"); } 

and:

CallMethod(ref testInstance); 

Hope this helps.

2 Comments

callmethod(obj) won't compile, it needs to be passed as a reference, so at a minimum it would have to be callmethod(ref obj).
Ah, yes I forgot about the method signature. Thanks for pointing it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.