Ejemplo n.º 1
0
 void ISerializationCallbackReceiver.OnAfterDeserialize()
 {
     // Attempt to preserve and restore the target type even if
     // it wasn't available during an assembly reload.
     if (targetType != null)
     {
         _targetTypeName = RuntimeCodebase.SerializeType(targetType);
     }
     else if (_targetTypeName != null)
     {
         try
         {
             targetType = RuntimeCodebase.DeserializeType(_targetTypeName);
         }
         catch { }
     }
 }
Ejemplo n.º 2
0
        public void Reflect()
        {
            // Cannot happen from the constructor, but will occur
            // if the type doesn't exist and fails to be deserialized
            if (targetType == null)
            {
                if (targetTypeName != null)
                {
                    throw new MissingMemberException(targetTypeName, name);
                }
                else
                {
                    throw new MissingMemberException("Target type not found.");
                }
            }

            _source = Source.Unknown;

            _fieldInfo       = null;
            _propertyInfo    = null;
            _methodInfo      = null;
            _constructorInfo = null;

            fieldAccessor    = null;
            propertyAccessor = null;
            methodInvoker    = null;

            var candidates = targetType.GetExtendedMember(name, SupportedMemberTypes, SupportedBindingFlags);

            if (candidates.Length == 0) // Not found, check if it might have been renamed
            {
                var renamedMembers = RuntimeCodebase.RenamedMembers(targetType);

                string newName;

                if (renamedMembers.TryGetValue(name, out newName))
                {
                    name = newName;

                    candidates = targetType.GetExtendedMember(name, SupportedMemberTypes, SupportedBindingFlags);
                }
            }

            if (candidates.Length == 0) // Nope, not even, abort
            {
                throw new MissingMemberException($"No matching member found: '{targetType.Name}.{name}'");
            }

            MemberTypes?memberType = null;

            foreach (var candidate in candidates)
            {
                if (memberType == null)
                {
                    memberType = candidate.MemberType;
                }
                else if (candidate.MemberType != memberType && !candidate.IsExtensionMethod())
                {
                    // This theoretically shouldn't happen according to the .NET specification, I believe
                    Debug.LogWarning($"Multiple members with the same name are of a different type: '{targetType.Name}.{name}'");
                    break;
                }
            }

            switch (memberType)
            {
            case MemberTypes.Field:
                ReflectField(candidates);
                break;

            case MemberTypes.Property:
                ReflectProperty(candidates);
                break;

            case MemberTypes.Method:
                ReflectMethod(candidates);
                break;

            case MemberTypes.Constructor:
                ReflectConstructor(candidates);
                break;

            default:
                throw new UnexpectedEnumValueException <MemberTypes>(memberType.Value);
            }

            isReflected = true;
        }
Ejemplo n.º 3
0
 public static Type DeserializeType(string typeName)
 {
     return(RuntimeCodebase.DeserializeType(typeName));
 }
Ejemplo n.º 4
0
 public static bool TryDeserializeType(string typeName, out Type type)
 {
     return(RuntimeCodebase.TryDeserializeType(typeName, out type));
 }
Ejemplo n.º 5
0
 public static string SerializeType(Type type)
 {
     return(RuntimeCodebase.SerializeType(type));
 }
Ejemplo n.º 6
0
        static Codebase()
        {
            using (ProfilingUtility.SampleBlock("Codebase initialization"))
            {
                _assemblies             = new List <Assembly>();
                _runtimeAssemblies      = new List <Assembly>();
                _editorAssemblies       = new List <Assembly>();
                _ludiqAssemblies        = new List <Assembly>();
                _ludiqRuntimeAssemblies = new List <Assembly>();
                _ludiqEditorAssemblies  = new List <Assembly>();

                _types             = new List <Type>();
                _runtimeTypes      = new List <Type>();
                _editorTypes       = new List <Type>();
                _ludiqTypes        = new List <Type>();
                _ludiqRuntimeTypes = new List <Type>();
                _ludiqEditorTypes  = new List <Type>();

                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    try
                    {
#if NET_4_6
                        if (assembly.IsDynamic)
                        {
                            continue;
                        }
#endif

                        _assemblies.Add(assembly);

                        var isRuntimeAssembly      = IsRuntimeAssembly(assembly);
                        var isEditorAssembly       = IsEditorDependentAssembly(assembly);
                        var isLudiqAssembly        = IsLudiqRuntimeDependentAssembly(assembly) || IsLudiqEditorDependentAssembly(assembly);
                        var isLudiqEditorAssembly  = IsLudiqEditorDependentAssembly(assembly);
                        var isLudiqRuntimeAssembly = IsLudiqRuntimeDependentAssembly(assembly) && !IsLudiqEditorDependentAssembly(assembly);

                        if (isRuntimeAssembly)
                        {
                            _runtimeAssemblies.Add(assembly);
                        }

                        if (isEditorAssembly)
                        {
                            _editorAssemblies.Add(assembly);
                        }

                        if (isLudiqAssembly)
                        {
                            _ludiqAssemblies.Add(assembly);
                        }

                        if (isLudiqEditorAssembly)
                        {
                            _ludiqEditorAssemblies.Add(assembly);
                        }

                        if (isLudiqRuntimeAssembly)
                        {
                            _ludiqRuntimeAssemblies.Add(assembly);
                        }

                        foreach (var type in assembly.GetTypesSafely())
                        {
                            _types.Add(type);

                            RuntimeCodebase.PrewarmTypeDeserialization(type);

                            if (isRuntimeAssembly)
                            {
                                _runtimeTypes.Add(type);
                            }

                            if (isEditorAssembly)
                            {
                                _editorTypes.Add(type);
                            }

                            if (isLudiqAssembly)
                            {
                                _ludiqTypes.Add(type);
                            }

                            if (isLudiqEditorAssembly)
                            {
                                _ludiqEditorTypes.Add(type);
                            }

                            if (isLudiqRuntimeAssembly)
                            {
                                _ludiqRuntimeTypes.Add(type);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogWarning($"Failed to analyze assembly '{assembly}':\n{ex}");
                    }
                }

                assemblies             = _assemblies.AsReadOnly();
                runtimeAssemblies      = _runtimeAssemblies.AsReadOnly();
                editorAssemblies       = _editorAssemblies.AsReadOnly();
                ludiqAssemblies        = _ludiqAssemblies.AsReadOnly();
                ludiqRuntimeAssemblies = _ludiqRuntimeAssemblies.AsReadOnly();
                ludiqEditorAssemblies  = _ludiqEditorAssemblies.AsReadOnly();

                types             = _types.AsReadOnly();
                runtimeTypes      = _runtimeTypes.AsReadOnly();
                editorTypes       = _editorTypes.AsReadOnly();
                ludiqTypes        = _ludiqTypes.AsReadOnly();
                ludiqRuntimeTypes = _ludiqRuntimeTypes.AsReadOnly();
                ludiqEditorTypes  = _ludiqEditorTypes.AsReadOnly();
            }
        }