为什么以下代码不起作用:
class Program
{
static void Main ( string[ ] args )
{
SomeClass s = new SomeClass( );
s.GetType( ).GetField( "id" , System.Reflection.BindingFlags.NonPublic ) // sorry reasently updated to GetField from GetProperty...
.SetValue( s , "new value" );
}
}
class SomeClass
{
object id;
public object Id
{
get
{
return id;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图设置私有字段的值.
这是我得到的例外:
System.NullReferenceException未处理Message = Object引用未设置为对象的实例.来源= ConsoleApplication7
StackTrace:位于C:\ Users\Antonio\Desktop\ConsoleApplication7\ConsoleApplication7\Program.cs中的Program.Main(String [] args):位于System的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)的第18行. System.Threading.ExecutionContext.Run上的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()中的AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)(ExecutionContext executionContext System.Threading.ThreadHelper.ThreadStart()中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态),ContextExallback回调,对象状态,布尔值ignoreSyncCtx:InnerException:
TypedReference在实际代码中实际使用的结构是否有任何实际用途?
编辑:.Net框架在重载中使用它们,Console.WriteLine并String.Concat从__arglist参数构建数组并将其传递给正常的params重载.为什么存在这些过载?
我正在尝试使用反射(最终在编译时未知)object,包括struct.我已经到了,TypedReference.MakeTypedReference但我已经撞墙了.
这是我的班级和结构
public class MyObject
{
public int Id;
public Money Amount;
}
public struct Money
{
public int Vaule;
public string Code;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何尝试使用反射在MyObject中设置"数量"的"代码".正如我上面提到的,我正在寻找一种在编译时不了解这些类型的解决方案(这太容易了!)
这是我到目前为止的代码(我使用[0],[1]来使代码更简单)
var obj = new MyObject() { Id = 1 };
obj.Amount.Vaule = 10;
obj.Amount.Code = "ABC";
FieldInfo[] objFields = obj.GetType().GetFields();
FieldInfo[] moneyFields = objFields[1].GetValue(obj).GetType().GetFields();
List<FieldInfo> fields = new List<FieldInfo>() { objFields[1] };
fields.AddRange( moneyFields );
TypedReference typeRef = TypedReference.MakeTypedReference(
objFields[1].GetValue( obj ), fields.ToArray() );
moneyFields[1].SetValueDirect( typeRef, "XXX" ); …Run Code Online (Sandbox Code Playgroud) class PriceClass {
private int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
}
struct PriceStruct
{
private int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
}
static void Main(string[] args)
{
PriceClass _priceClass = new PriceClass();
Type type = typeof(PriceClass);
PropertyInfo info = type.GetProperty("Value");
info.SetValue(_priceClass, 32, null);
Console.WriteLine(_priceClass.Value);
PriceStruct _priceStruct = new PriceStruct();
type = typeof(PriceStruct);
info = type.GetProperty("Value");
info.SetValue(_priceStruct, …Run Code Online (Sandbox Code Playgroud) 我正在编写自己的方法将对象图转换为自定义对象,因为JavaScriptSerializer会在空值上触发错误.
所以这就是我到目前为止所做的:
internal static T ParseObjectGraph<T>(Dictionary<string, object> oGraph)
{
T generic = (T)Activator.CreateInstance<T>();
Type resType = typeof(T);
foreach (PropertyInfo pi in resType.GetProperties())
{
object outObj = new object();
if (oGraph.TryGetValue(pi.Name.ToLower(), out outObj))
{
Type outType = outObj.GetType();
if (outType == pi.PropertyType)
{
pi.SetValue(generic, outObj, null);
}
}
}
return generic;
}
Run Code Online (Sandbox Code Playgroud)
现在该pi.SetValue()方法运行,并且不会触发错误,但是当我查看属性时generic,它仍然与之前的相同.
它经历的第一个属性是布尔值,因此值最终会像这样
generic = an object of type MyCustomType
generic.property = false
outObj = true
pi = boolean property
outType = boolean
Run Code Online (Sandbox Code Playgroud)
然后在SetValue方法运行后, …