protected ICodeBlock GetDestroyForReferencedFile(IElement element, ReferencedFileSave referencedFile)
        {
            ICodeBlock codeBlock = new CodeDocument(3);

            ///////////////////////////////EARLY OUT///////////////////////
            if (!referencedFile.LoadedAtRuntime || !referencedFile.DestroyOnUnload)
            {
                return codeBlock;
            }

            if (referencedFile.GetGeneratesMember() == false)
            {
                return codeBlock;
            }

            /////////////////////////////END EARLY OUT/////////////////////

            AddIfConditionalSymbolIfNecesssary(codeBlock, referencedFile);

            string fileName = referencedFile.Name;
            AssetTypeInfo ati = referencedFile.GetAssetTypeInfo();
            string variableName = referencedFile.GetInstanceName();

            bool isScreenSave = element is ScreenSave;
            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                variableName = "m" + variableName;
                codeBlock = codeBlock.If(variableName + " != null");
            }
            if (ati != null && (!referencedFile.IsSharedStatic || isScreenSave))
            {
                string typeName = ati.RuntimeTypeName;
                string destroyMethod = ati.DestroyMethod;
                string recycleMethod = ati.RecycledDestroyMethod;
                if (string.IsNullOrEmpty(recycleMethod))
                {
                    recycleMethod = destroyMethod;
                }



                if (!string.IsNullOrEmpty(ati.DestroyMethod))
                {
                    if (isScreenSave && recycleMethod != destroyMethod)
                    {
                        codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                        codeBlock.Line(destroyMethod.Replace("this", variableName) + ";");
                        if (referencedFile.LoadedOnlyWhenReferenced)
                        {
                            codeBlock = codeBlock.End().ElseIf(variableName + " != null");
                        }
                        else
                        {
                            codeBlock = codeBlock.End().Else();
                        }
                        codeBlock.Line(recycleMethod.Replace("this", variableName) + ";");
                        codeBlock = codeBlock.End();

                    }
                    else
                    {
                        codeBlock.Line(destroyMethod.Replace("this", variableName) + ";");
                    }
                }

                if (ati.ShouldBeDisposed && element.UseGlobalContent == false)
                {
                    codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                    codeBlock.Line(string.Format("{0}.Dispose();", variableName));
                    codeBlock = codeBlock.End();
                }

                
            }

            if (element is ScreenSave && referencedFile.IsSharedStatic)
            {
                if (referencedFile.LoadedOnlyWhenReferenced)
                {
                    variableName = "m" + referencedFile.GetInstanceName();
                }
                // We used to do this here, but we want to do it after all Objects have been destroyed
                // because we may need to make the file one way before the destruction of the objects.

                if (ati != null && ati.SupportsMakeOneWay)
                {
                    codeBlock = codeBlock.If("this.UnloadsContentManagerWhenDestroyed && ContentManagerName != \"Global\"");
                    codeBlock.Line(string.Format("{0} = null;", variableName));
                    codeBlock = codeBlock.End().Else();
                    codeBlock.Line(string.Format("{0}.MakeOneWay();", variableName));
                    codeBlock = codeBlock.End();

                }
                else
                {
                    codeBlock.Line(string.Format("{0} = null;", variableName));

                }
            }

            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                codeBlock = codeBlock.End();
            }
            AddEndIfIfNecessary(codeBlock, referencedFile);
            return codeBlock;
        }
        public static void GetInitializationForReferencedFile(ReferencedFileSave referencedFile, IElement container, 
            ICodeBlock codeBlock,  bool loadsUsingGlobalContentManager, LoadType loadType)
        {
            #region early-outs (not loaded at runtime, loaded only when referenced)

            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                return;// "";
            }
            
            if (referencedFile.IsDatabaseForLocalizing == false && !referencedFile.GetGeneratesMember())
            {
                return; // There is no qualified type to load to, so let's not generate code to load it
            }

            #endregion

            // I'm going to only do this if we're non-null so that we don't add it for global content.  Global Content may load
            // async and cause bad data
            if (container != null)
            {
                PerformancePluginCodeGenerator.GenerateStart(container, codeBlock, "LoadStaticContent" + FileManager.RemovePath(referencedFile.Name));
            }
            AddIfConditionalSymbolIfNecesssary(codeBlock, referencedFile);

            bool directives = false;

            for (int i = referencedFile.ProjectSpecificFiles.Count; i >= 0; i--)
            {
                bool isProjectSpecific = i != 0;

                string fileName;
                ProjectBase project;

                if (isProjectSpecific)
                {
                    fileName = referencedFile.ProjectSpecificFiles[i - 1].FilePath.ToLower().Replace("\\", "/");

                    // At one point
                    // the project specific
                    // files were platform specific
                    // but instead we want them to be
                    // based off of the project name instead.
                    // The reason for this is because a user could
                    // create a synced project that targets the same
                    // platform.  
                    project = ProjectManager.GetProjectByName(referencedFile.ProjectSpecificFiles[i - 1].ProjectId);
                    if (project == null)
                    {
                        project = ProjectManager.GetProjectByTypeId(referencedFile.ProjectSpecificFiles[i - 1].ProjectId);
                    }
                }
                else
                {
                    fileName = referencedFile.Name.ToLower().Replace("\\", "/");
                    project = ProjectManager.ProjectBase;
                }

                string containerName = ContentLoadWriter.GlobalContentContainerName;
                if (container != null)
                {
                    containerName = container.Name;
                }
                AddCodeforFileLoad(referencedFile, ref codeBlock, loadsUsingGlobalContentManager, 
                    ref directives, isProjectSpecific, ref fileName, project, loadType, containerName);
            }

            if (directives == true)
            {
                codeBlock = codeBlock.End()
                    .Line("#endif");
            }

            AddEndIfIfNecessary(codeBlock, referencedFile);
            // See above why this if-statement exists
            if (container != null)
            {
                PerformancePluginCodeGenerator.GenerateEnd(container, codeBlock, "LoadStaticContent" + FileManager.RemovePath(referencedFile.Name));
            }
        }
        private static bool GetIfShouldGenerateInitialize(ReferencedFileSave referencedFile)
        {
            bool shouldGenerateInitialize = true;

            if (referencedFile.LoadedOnlyWhenReferenced)
            {
                shouldGenerateInitialize = false;
            }

            if (referencedFile.IsDatabaseForLocalizing == false && !referencedFile.GetGeneratesMember())
            {
                shouldGenerateInitialize = false; // There is no qualified type to load to, so let's not generate code to load it
            }

            if (referencedFile.IsDatabaseForLocalizing && !referencedFile.LoadedAtRuntime)
            {
                shouldGenerateInitialize = false;
            }

            return shouldGenerateInitialize;
        }