I need to read objects binary data (including private fields) to process and serialize them in a specific way.
How can I do this in C#, do I need MSIL coding?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do it using reflection and (optionally) dynamic IL generation.
For example, once you know the type (i.e. have a
System.Typeinstance), you can enumerate all fields (type.GetFields()returns a list ofFieldInfoobjects), and then useGetValuemethod to get field value. This works with private fields, as long as the security trust level checks are passed.This is not very fast, so you may want to precompile the field access code (only do that after the profiler tells you!). In this case, you can use
System.Reflection.EmitandDynamicMethodfacilities. (you can find the tutorials on Google and on MSDN; I found it helpful to compile some functions that do what I have to do with C#/F# and then inspect the MSIL output in Reflector/ildasm).