I am guessing the answer will eventually be "no, you can't do that", but trying to figure out how to do a thing in C#.
I am wondering on a bit of glue code between two pieces that I do not have any control over. On the data side I have something like this:
public struct StructA
{
public float varA;
}
public class ClassA
{
public struct StructB
{
public StructA? varB;
}
private StructB? varC;
}
And then finally I have this function:
public static void FunctionA(ref float value)
This is a function that I do not control, which wants a reference to a float. If I have a StructA and pass StructA.varA into FunctionA, it correctly operates on varA in place.
I have a float nested down in two nullable structs, and a function that will only take a reference to a float. I've tried using reflection to get the field info for the value inside the nullable wrapper, but it is behaving like GetValue is returning, well, the whole struct instead of a reference.
The only next step I can think of is trying something with an unsafe block and some explicit casting? I keep hitting half solutions where the compiler admits it knows what I am trying to do, but will not let me - is there some way to tell it to just do it?
(adding)
An example of how I've been trying to approach it:
StructA exampleA = (StructA)typeof(StructA?).GetField("value",BindingFlags.NonPublic|BindingFlags.Instance).GetValue(SomeNullableStructA);
I've also tried
unsafe
{
StructA* ptr = (StructA*)&SomeNullableStructA;
}
Which is not taking into acount Nullable.hasValue, so I would not expect that to line up, but I didn't go too far down this path due to the error:
C8370 : Feature 'unmanaged constructed types' is not available in C# 7.3. Please use language version 8.0 or greater.
Since this is a dll loading into someone else's app, and it wants .NET framework 4.7.2, I am assuming I can not mix them.
varAin place, or would it be sufficient to get it, operate on it, and set it back?GetValue()method but your classes/structs does not have such method.varB. It's pretty hard to get that from your text. Please share some basic sample-code that shows what exactly you're doing.ca.varC.Value.varA, the fieldca.varCwill still be null. That's becauseNullable<T>contains two fields,internal T value;andprivate bool hasValue;and, while you have mutated the contents ofvarC.Value,ca.varC.hasValueis still false, see dotnetfiddle.net/u8LyOG.Nullable<T>struct without considering thehasValuebool, so the querent really can't do what they want in a single call. Somehow they must either get, modify, and set the containing nullable, or mutate thehasValueas well.