Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my scenario.
I have a MyClass class and protected field _myAttempt which is a list.

_myAttempt list class looks like below.

From MyClassTest, how can i assign value to Daily using Reflection.

I have tried following under MyClassTest using reflection but I am not able to assign object to SetValue.

Any help would be appreciated.

What I have tried:

internal class MyClass {
protected List<attempt> _myAttempt = null;
}

public class Attempt {
public string Daily {get;set;}
public string Weekly {get;set;}
}

public class MyClassTest {
[TestMethod]
public void TestDaily(){
var fieldInfo = typeof(MyClass).GetField("_myAttempt", BindingFlags.NonPublic |
BindingFlags.Instance);
fieldInfo.SetValue(obj,value); //not sure what is object here and how to assign

}
}
Posted
Updated 20-Oct-20 21:28pm
Comments
BillWoodruff 21-Oct-20 5:48am    
the code you show will never compile.

1 solution

In your case obj is instance of MyClass and value is list you wish to pass.
C#
var obj = new MyClass();
var newValue = new List<attempt>();

fieldInfo.SetValue(obj, newValue );

Read more about it.
 
Share this answer
 
Comments
BillWoodruff 21-Oct-20 14:56pm    
vote of #1: this code is as useless as the OP's

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900