+
+ return CollectFiles (appCode, defasm);
+ }
+
+ CodeTypeReference GetProfilePropertyType (string type)
+ {
+ if (String.IsNullOrEmpty (type))
+ throw new ArgumentException ("String size cannot be 0", "type");
+ return new CodeTypeReference (type);
+ }
+
+ string FindProviderTypeName (ProfileSection ps, string providerName)
+ {
+ if (ps.Providers == null || ps.Providers.Count == 0)
+ return null;
+
+ ProviderSettings pset = ps.Providers [providerName];
+ if (pset == null)
+ return null;
+ return pset.Type;
+ }
+
+ void GetProfileProviderAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
+ string providerName)
+ {
+ if (String.IsNullOrEmpty (providerName))
+ providerTypeName = FindProviderTypeName (ps, ps.DefaultProvider);
+ else
+ providerTypeName = FindProviderTypeName (ps, providerName);
+ if (providerTypeName == null)
+ throw new HttpException (String.Format ("Profile provider type not defined: {0}",
+ providerName));
+
+ collection.Add (
+ new CodeAttributeDeclaration (
+ "ProfileProvider",
+ new CodeAttributeArgument (
+ new CodePrimitiveExpression (providerTypeName)
+ )
+ )
+ );
+ }
+
+ void GetProfileSettingsSerializeAsAttribute (ProfileSection ps, CodeAttributeDeclarationCollection collection,
+ SerializationMode mode)
+ {
+ string parameter = String.Concat ("SettingsSerializeAs.", mode.ToString ());
+ collection.Add (
+ new CodeAttributeDeclaration (
+ "SettingsSerializeAs",
+ new CodeAttributeArgument (
+ new CodeSnippetExpression (parameter)
+ )
+ )
+ );
+
+ }
+
+ void AddProfileClassGetProfileMethod (CodeTypeDeclaration profileClass)
+ {
+ CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
+ new CodeTypeReferenceExpression (typeof (System.Web.Profile.ProfileBase)),
+ "Create");
+ CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
+ mref,
+ new CodeExpression[] { new CodeVariableReferenceExpression ("username") }
+ );
+ CodeCastExpression cast = new CodeCastExpression ();
+ cast.TargetType = new CodeTypeReference ("ProfileCommon");
+ cast.Expression = minvoke;
+
+ CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
+ ret.Expression = cast;
+
+ CodeMemberMethod method = new CodeMemberMethod ();
+ method.Name = "GetProfile";
+ method.ReturnType = new CodeTypeReference ("ProfileCommon");
+ method.Parameters.Add (new CodeParameterDeclarationExpression("System.String", "username"));
+ method.Statements.Add (ret);
+ method.Attributes = MemberAttributes.Public;
+
+ profileClass.Members.Add (method);
+ }
+
+ void AddProfileClassProperty (ProfileSection ps, CodeTypeDeclaration profileClass, ProfilePropertySettings pset)
+ {
+ string name = pset.Name;
+ if (String.IsNullOrEmpty (name))
+ throw new HttpException ("Profile property 'Name' attribute cannot be null.");
+ CodeMemberProperty property = new CodeMemberProperty ();
+ string typeName = pset.Type;
+ if (typeName == "string")
+ typeName = "System.String";
+ property.Name = name;
+ property.Type = GetProfilePropertyType (typeName);
+ property.Attributes = MemberAttributes.Public;
+
+ CodeAttributeDeclarationCollection collection = new CodeAttributeDeclarationCollection();
+ GetProfileProviderAttribute (ps, collection, pset.Provider);
+ GetProfileSettingsSerializeAsAttribute (ps, collection, pset.SerializeAs);
+
+ property.CustomAttributes = collection;
+ CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
+ CodeCastExpression cast = new CodeCastExpression ();
+ ret.Expression = cast;
+
+ CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
+ new CodeThisReferenceExpression (),
+ "GetPropertyValue");
+ CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
+ mref,
+ new CodeExpression[] { new CodePrimitiveExpression (name) }
+ );
+ cast.TargetType = new CodeTypeReference (typeName);
+ cast.Expression = minvoke;
+ property.GetStatements.Add (ret);
+
+ if (!pset.ReadOnly) {
+ mref = new CodeMethodReferenceExpression (
+ new CodeThisReferenceExpression (),
+ "SetPropertyValue");
+ minvoke = new CodeMethodInvokeExpression (
+ mref,
+ new CodeExpression[] { new CodePrimitiveExpression (name), new CodeSnippetExpression ("value") }
+ );
+ property.SetStatements.Add (minvoke);
+ }
+
+
+ profileClass.Members.Add (property);
+ }
+
+ void AddProfileClassGroupProperty (string groupName, string memberName, CodeTypeDeclaration profileClass)
+ {
+ CodeMemberProperty property = new CodeMemberProperty ();
+ property.Name = memberName;
+ property.Type = new CodeTypeReference (groupName);
+ property.Attributes = MemberAttributes.Public;
+
+ CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
+ CodeCastExpression cast = new CodeCastExpression ();
+ ret.Expression = cast;
+
+ CodeMethodReferenceExpression mref = new CodeMethodReferenceExpression (
+ new CodeThisReferenceExpression (),
+ "GetProfileGroup");
+ CodeMethodInvokeExpression minvoke = new CodeMethodInvokeExpression (
+ mref,
+ new CodeExpression[] { new CodePrimitiveExpression (memberName) }
+ );
+ cast.TargetType = new CodeTypeReference (groupName);
+ cast.Expression = minvoke;
+ property.GetStatements.Add (ret);
+
+ profileClass.Members.Add (property);
+ }
+
+ void BuildProfileClass (ProfileSection ps, string className, ProfilePropertySettingsCollection psc,
+ CodeNamespace ns, string baseClass, bool baseIsGlobal,
+ SortedList <string, string> groupProperties)
+ {
+ CodeTypeDeclaration profileClass = new CodeTypeDeclaration (className);
+ CodeTypeReference cref = new CodeTypeReference (baseClass);
+ if (baseIsGlobal)
+ cref.Options |= CodeTypeReferenceOptions.GlobalReference;
+ profileClass.BaseTypes.Add (cref);
+ profileClass.TypeAttributes = TypeAttributes.Public;
+ ns.Types.Add (profileClass);
+
+ foreach (ProfilePropertySettings pset in psc)
+ AddProfileClassProperty (ps, profileClass, pset);
+ if (groupProperties != null && groupProperties.Count > 0)
+ foreach (KeyValuePair <string, string> group in groupProperties)
+ AddProfileClassGroupProperty (group.Key, group.Value, profileClass);
+ AddProfileClassGetProfileMethod (profileClass);
+ }
+
+ string MakeGroupName (string name)
+ {
+ return String.Concat ("ProfileGroup", name);
+ }
+
+ // FIXME: there should be some validation of syntactic correctness of the member/class name
+ // for the groups/properties. For now it's left to the compiler to report errors.
+ //
+ // CodeGenerator.IsValidLanguageIndependentIdentifier (id) - use that
+ //
+ bool ProcessCustomProfile (ProfileSection ps, AppCodeAssembly defasm)
+ {
+ CodeCompileUnit unit = new CodeCompileUnit ();
+ CodeNamespace ns = new CodeNamespace (null);
+ unit.Namespaces.Add (ns);
+ defasm.AddUnit (unit);
+
+ ns.Imports.Add (new CodeNamespaceImport ("System"));
+ ns.Imports.Add (new CodeNamespaceImport ("System.Configuration"));
+ ns.Imports.Add (new CodeNamespaceImport ("System.Web"));
+ ns.Imports.Add (new CodeNamespaceImport ("System.Web.Profile"));
+
+ RootProfilePropertySettingsCollection props = ps.PropertySettings;
+ if (props == null)
+ return true;
+
+ SortedList<string, string> groupProperties = new SortedList<string, string> ();
+ string groupName;
+ foreach (ProfileGroupSettings pgs in props.GroupSettings) {
+ groupName = MakeGroupName (pgs.Name);
+ groupProperties.Add (groupName, pgs.Name);
+ BuildProfileClass (ps, groupName, pgs.PropertySettings, ns,
+ "System.Web.Profile.ProfileGroupBase", true, null);
+ }
+
+ string baseType = ps.Inherits;
+ if (String.IsNullOrEmpty (baseType))
+ baseType = "System.Web.Profile.ProfileBase";
+ else {
+ string[] parts = baseType.Split (new char[] {','});
+ if (parts.Length > 1)
+ baseType = parts [0].Trim ();
+ }
+
+ bool baseIsGlobal;
+ if (baseType.IndexOf ('.') != -1)
+ baseIsGlobal = true;
+ else
+ baseIsGlobal = false;
+
+ BuildProfileClass (ps, "ProfileCommon", props, ns, baseType, baseIsGlobal, groupProperties);
+ return true;
+ }
+
+// void PutCustomProfileInContext (HttpContext context, string assemblyName)
+// {
+// Type type = Type.GetType (String.Format ("ProfileCommon, {0}",
+// Path.GetFileNameWithoutExtension (assemblyName)));
+// ProfileBase pb = Activator.CreateInstance (type) as ProfileBase;
+// if (pb != null)
+// context.Profile = pb;
+// }
+
+ public static bool HaveCustomProfile (ProfileSection ps)
+ {
+ if (ps == null || !ps.Enabled)
+ return false;
+
+ RootProfilePropertySettingsCollection props = ps.PropertySettings;
+ ProfileGroupSettingsCollection groups = props != null ? props.GroupSettings : null;
+
+ if (!String.IsNullOrEmpty (ps.Inherits) || (props != null && props.Count > 0) || (groups != null && groups.Count > 0))
+ return true;
+
+ return false;
+ }
+
+ public void Compile ()
+ {
+ if (_alreadyCompiled)
+ return;
+
+ string appCode = Path.Combine (HttpRuntime.AppDomainAppPath, "App_Code");
+ ProfileSection ps = WebConfigurationManager.GetWebApplicationSection ("system.web/profile") as ProfileSection;
+ bool haveAppCodeDir = Directory.Exists (appCode);
+ bool haveCustomProfile = HaveCustomProfile (ps);
+
+ if (!haveAppCodeDir && !haveCustomProfile)
+ return;
+