/// <summary> /// Gets the load from file CodeDOM method. /// </summary> /// <param name="type">The type CodeTypeDeclaration.</param> /// <returns>return the codeDom LoadFromFile method</returns> protected override CodeMemberMethod GetLoadFromFileMethod(CodeTypeDeclaration type) { var typeName = GeneratorContext.GeneratorParams.GenericBaseClass.Enabled ? "T" : type.Name; // --------------------------------------------- // public static T LoadFromFile(string fileName) // --------------------------------------------- var loadFromFileMethod = new CodeMemberMethod { Attributes = MemberAttributes.Public | MemberAttributes.Static, Name = GeneratorContext.GeneratorParams.Serialization.LoadFromFileMethodName }; loadFromFileMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string), "fileName")); if (GeneratorContext.GeneratorParams.Serialization.EnableEncoding) { loadFromFileMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Encoding), "encoding")); } loadFromFileMethod.ReturnType = new CodeTypeReference(typeName); loadFromFileMethod.Statements.Add( new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(IsolatedStorageFile)), "isoFile", new CodePrimitiveExpression(null))); loadFromFileMethod.Statements.Add( new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(IsolatedStorageFileStream)), "isoStream", new CodePrimitiveExpression(null))); loadFromFileMethod.Statements.Add( new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(StreamReader)), "sr", new CodePrimitiveExpression(null))); var tryStatmanentsCol = new CodeStatementCollection(); var finallyStatmanentsCol = new CodeStatementCollection(); tryStatmanentsCol.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("isoFile"), CodeDomHelper.GetInvokeMethod("IsolatedStorageFile", "GetUserStoreForApplication"))); tryStatmanentsCol.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("isoStream"), new CodeObjectCreateExpression( typeof(IsolatedStorageFileStream), new CodeExpression[] { new CodeArgumentReferenceExpression("fileName"), CodeDomHelper.GetEnum("FileMode", "Open"), new CodeVariableReferenceExpression("isoFile") }))); CodeExpression[] codeParamExpression; if (GeneratorContext.GeneratorParams.Serialization.EnableEncoding) { codeParamExpression = new CodeExpression[] { new CodeVariableReferenceExpression("isoStream"), new CodeVariableReferenceExpression("encoding") }; } else { codeParamExpression = new CodeExpression[] { new CodeVariableReferenceExpression("isoStream") }; } tryStatmanentsCol.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("sr"), new CodeObjectCreateExpression( typeof(StreamReader), codeParamExpression ))); // ---------------------------------- // string xmlString = sr.ReadToEnd(); // ---------------------------------- var readToEndInvoke = CodeDomHelper.GetInvokeMethod("sr", "ReadToEnd"); var xmlString = new CodeVariableDeclarationStatement( new CodeTypeReference(typeof(string)), "xmlString", readToEndInvoke); tryStatmanentsCol.Add(xmlString); tryStatmanentsCol.Add(CodeDomHelper.GetInvokeMethod("isoStream", "Close")); tryStatmanentsCol.Add(CodeDomHelper.GetInvokeMethod("sr", "Close")); // ------------------------------------------------------ // return Deserialize(xmlString, out obj, out exception); // ------------------------------------------------------ var fileName = new CodeVariableReferenceExpression("xmlString"); var deserializeInvoke = new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(null, GeneratorContext.GeneratorParams.Serialization.DeserializeMethodName), new CodeExpression[] { fileName }); var rstmts = new CodeMethodReturnStatement(deserializeInvoke); tryStatmanentsCol.Add(rstmts); finallyStatmanentsCol.Add(CodeDomHelper.GetDispose("isoFile")); finallyStatmanentsCol.Add(CodeDomHelper.GetDispose("isoStream")); finallyStatmanentsCol.Add(CodeDomHelper.GetDispose("sr")); var tryfinally = new CodeTryCatchFinallyStatement( CodeDomHelper.CodeStmtColToArray(tryStatmanentsCol), new CodeCatchClause[0], CodeDomHelper.CodeStmtColToArray(finallyStatmanentsCol)); loadFromFileMethod.Statements.Add(tryfinally); return(loadFromFileMethod); }