Exemplo n.º 1
0
 public void Ctor_ThrowsArgumentNull()
 {
     assert.Throws <ArgumentNullException>(() =>
     {
         _ = new ReflectionField(null);
     });
 }
Exemplo n.º 2
0
    public void FindAttribute()
    {
        var first = ReflectData
                    .GetFields()
                    .FirstOrDefault();
        var reflect = new ReflectionField(first);

        var attr = reflect.FindAttribute <DecoratorAttribute>();

        assert.NotNull(attr);
    }
Exemplo n.º 3
0
    public void FindAttributes()
    {
        var first = ReflectData
                    .GetFields()
                    .FirstOrDefault();
        var reflect = new ReflectionField(first);

        var attrs = reflect.FindAttributes <DecoratorAttribute>();

        assert.NotNull(attrs);
        assert.Equal(1, attrs.Count);
    }
 private void SetupReflection(Assembly Assembly)
 {
     try
     {
         m_startupArgsField = new ReflectionField("CommandLineArgs", ClassName, m_classType);
         m_startupMethod    = new ReflectionMethod(InitMethod, ClassName, m_classType);
         m_stopMethod       = new ReflectionMethod(StopMethod, ClassName, m_classType);
     }
     catch (ArgumentException ex)
     {
         mainLog.Error(ex.ToString());
     }
 }
Exemplo n.º 5
0
    public void Private_GetValue()
    {
        var first = typeof(ReflectData).GetField(
            "Field4",
            BindingFlags.NonPublic | BindingFlags.Instance);

        assert.NotNull(first);

        var reflect = new ReflectionField(first);
        var data    = new ReflectData();

        assert.Equal(10, (int)reflect.GetValue(data));
    }
Exemplo n.º 6
0
    public void GetValue()
    {
        var first = ReflectData
                    .GetFields()
                    .FirstOrDefault();
        var reflect = new ReflectionField(first);

        var data = new ReflectData()
        {
            Field2 = true
        };

        assert.Ok((bool)reflect.GetValue(data));
    }
Exemplo n.º 7
0
    public void SetValue()
    {
        var first = ReflectData
                    .GetFields()
                    .FirstOrDefault();
        var reflect = new ReflectionField(first);

        var data = new ReflectData()
        {
            Field2 = true
        };

        assert.True(data.Field2);

        reflect.SetValue(data, false);
        assert.False(data.Field2);
    }
Exemplo n.º 8
0
    private static void Set(System.Type type, object targetObject, string name, object input)
    {
        if (GetMember <MemberInfo[]>(targetObject, name).FirstOrDefault() == null)
        {
            if (reflectionFieldNames.Keys.Select(i => i.ToString().ToLower()).Contains(name))
            {
                MonoBehaviour   monoInstance = (MonoBehaviour)targetObject;
                ReflectionField targetField  = (ReflectionField)reflectionFieldNames.Keys.ToArray().GetValue(reflectionFieldNames.Values.ToList().IndexOf(name));

                if (setCustomActions.ContainsKey(targetField))
                {
                    setCustomActions[targetField](targetObject, input);
                }
                else if (enableLogging)
                {
                    Debug.Log($"No custom setter found for {targetField}");
                }
            }
        }
        else
        {
            Action <object, string, object> setAction;

            try
            {
                if (setMemberActions.TryGetValue(type, out setAction))
                {
                    setAction(targetObject, name, input);
                }
                else if (enableLogging)
                {
                    Debug.Log($"No member information found for {name}");
                }
            }
            catch (NullReferenceException ex)
            {
                if (enableLogging)
                {
                    Debug.Log($"Field {name} not found in instace: {ex.Message}");
                }
            }
        }
    }
Exemplo n.º 9
0
        private static bool CompareTypes(System.Type schemaType, ReflectionType reflectionType)
        {
            FieldInfo[] fields          = schemaType.GetFields();
            int         typedFieldCount = 0;

            string fieldNames = "";

            for (int i = 0; i < fields.Length; i++)
            {
                fieldNames += fields[i].Name + ", ";
            }

            foreach (FieldInfo field in fields)
            {
                object[] typeAttributes = field.GetCustomAttributes(typeof(Type), true);

                if (typeAttributes.Length == 1)
                {
                    Type            typedField      = (Type)typeAttributes[0];
                    ReflectionField reflectionField = reflectionType.fields[typedField.Index];

                    if (
                        reflectionField == null ||
                        reflectionField.type.IndexOf(typedField.FieldType) != 0 ||
                        reflectionField.name != field.Name
                        )
                    {
                        return(false);
                    }

                    typedFieldCount++;
                }
            }

            // skip if number of Type'd fields doesn't match
            if (typedFieldCount != reflectionType.fields.Count)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
0
    public void Ctor()
    {
        var first = ReflectData
                    .GetFields()
                    .FirstOrDefault();
        var reflectionField = new ReflectionField(first);

        assert.Equal(first.Name, reflectionField.Name);
        assert.Equal(first.FieldType, reflectionField.ClrType);
        assert.Equal(true, reflectionField.CanWrite);
        assert.Equal(true, reflectionField.CanRead);

        var mods = reflectionField.ModifierAccess;

        assert.Equal(true, mods.IsPublic);
        assert.Equal(false, mods.IsPrivate);
        assert.Equal(false, mods.IsInternal);
        assert.Equal(false, mods.IsVirtual);
        assert.Equal(false, mods.IsStatic);
        assert.Equal(true, mods.IsInstance);
    }
Exemplo n.º 11
0
 internal CommonField(CommonAttribute[] attributes, MonoCecilField monoCecilField, ReflectionField reflectionField)
 {
     Attributes      = attributes;
     MonoCecilField  = monoCecilField;
     ReflectionField = reflectionField;
 }