Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow field layout to be determined for more types #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Jil/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ public static Dictionary<FieldInfo, int> FieldOffsetsInMemory(Type t)

var getAddrs = emit.CreateDelegate(Utils.DelegateOptimizationOptions);

#if NETCORE
// When FormatterServices isn't available on the target platform,
// create an object of the type by invoking one of it's constructors
// with default values for each of the parameters.
// NOTE: FormatterServices is available on .NET Framework 1.1+ and netstandard2.0.
var cons = t.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(p => p.GetParameters().Count()).FirstOrDefault();
var consParameters = cons != null ? cons.GetParameters().Select(p => p.ParameterType.DefaultValue()).ToArray() : null;

Expand All @@ -486,6 +491,13 @@ public static Dictionary<FieldInfo, int> FieldOffsetsInMemory(Type t)
{
obj = Activator.CreateInstance(t);
}
#else
// Get an uninitialized object of the specified type.
// This is preferable to invoking a constructor of the type -- this method does not
// invoke any constructor for the type, so allows the field layout to be determined
// even when a type has constructors which e.g. check-then-throw for null arguments.
object obj = FormatterServices.GetUninitializedObject(t);
#endif

var addrs = getAddrs(obj);

Expand Down