<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;
using System.Data.Linq.Mapping;
using System.Reflection.Emit;

namespace DbLinq.Data.Linq.Mapping
{
	static class LambdaMetaAccessor
	{
		//This will go away with C# 4.0 ActionExpression
		static Delegate MakeSetter(MemberInfo member, Type memberType, Type declaringType)
		{
			Type delegateType = typeof(Action<,>).MakeGenericType(declaringType, memberType);

			switch (member.MemberType)
			{
				case MemberTypes.Property: {
                    MethodInfo method = ((PropertyInfo)member).GetSetMethod();
                    if (method != null)
                        return Delegate.CreateDelegate(delegateType, method);
                    var ca = member.GetCustomAttributes(typeof(ColumnAttribute), true)[0] as ColumnAttribute;
                    member = declaringType.GetField(ca.Storage,
                        BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                    goto case MemberTypes.Field;
                }
				case MemberTypes.Field:
					{
						DynamicMethod m = new DynamicMethod("setter", typeof(void), new Type[] { declaringType, memberType });
						ILGenerator cg = m.GetILGenerator();
					
						// arg0.<field> = arg1
						cg.Emit(OpCodes.Ldarg_0);
						cg.Emit(OpCodes.Ldarg_1);
						cg.Emit(OpCodes.Stfld, (FieldInfo)member);
						cg.Emit(OpCodes.Ret);
					
						return m.CreateDelegate(delegateType);
					}
				case MemberTypes.Method:
					 return Delegate.CreateDelegate(delegateType, (MethodInfo)member);
				default:
					throw new InvalidOperationException();
			}
		}

		public static MetaAccessor Create(MemberInfo member, Type declaringType)
		{
			Type memberType;
			switch (member.MemberType)
			{
				case MemberTypes.Property:<commit_msg>Set the DynamicMethod constructor restrictedSkipVisibility parameter to true. This allows accessibility checks to be skipped (if possible), and (more importantly) allows Mono to run more of the unit tests w/o throwing FieldAccessExceptions due to the DynamicMethod usage.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;
using System.Data.Linq.Mapping;
using System.Reflection.Emit;

namespace DbLinq.Data.Linq.Mapping
{
	static class LambdaMetaAccessor
	{
		//This will go away with C# 4.0 ActionExpression
		static Delegate MakeSetter(MemberInfo member, Type memberType, Type declaringType)
		{
			Type delegateType = typeof(Action<,>).MakeGenericType(declaringType, memberType);

			switch (member.MemberType)
			{
				case MemberTypes.Property: {
                    MethodInfo method = ((PropertyInfo)member).GetSetMethod();
                    if (method != null)
                        return Delegate.CreateDelegate(delegateType, method);
                    var ca = member.GetCustomAttributes(typeof(ColumnAttribute), true)[0] as ColumnAttribute;
                    member = declaringType.GetField(ca.Storage,
                        BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                    goto case MemberTypes.Field;
                }
				case MemberTypes.Field:
					{
						DynamicMethod m = new DynamicMethod("setter", 
                            typeof(void), 
                            new Type[] { declaringType, memberType },
                            true);
						ILGenerator cg = m.GetILGenerator();
					
						// arg0.<field> = arg1
						cg.Emit(OpCodes.Ldarg_0);
						cg.Emit(OpCodes.Ldarg_1);
						cg.Emit(OpCodes.Stfld, (FieldInfo)member);
						cg.Emit(OpCodes.Ret);
					
						return m.CreateDelegate(delegateType);
					}
				case MemberTypes.Method:
					 return Delegate.CreateDelegate(delegateType, (MethodInfo)member);
				default:
					throw new InvalidOperationException();
			}
		}

		public static MetaAccessor Create(MemberInfo member, Type declaringType)
		{
			Type memberType;
			switch (member.MemberType)
			{
				case MemberTypes.Property:
{'fraction_non_alphanumeric': 0.09049447911665867, 'fraction_numerical': 0.003360537686029765, 'mean_word_length': 2.757439134355275, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885010', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1105, 'n_words': 264}