Exemplo n.º 1
0
        static void HandlePolicyTypeUpdated(object sender, ExtensionNodeEventArgs args)
        {
            Type t = ((DataTypeCodon)args.ExtensionNode).Class;

            if (t == null)
            {
                throw new UserException("Type '" + ((DataTypeCodon)args.ExtensionNode).TypeName
                                        + "' not found. It could not be registered as a serializable type.");
            }

            string name = null;

            object[] obs = t.GetCustomAttributes(typeof(DataItemAttribute), true);
            if (obs != null && obs.Length > 0)
            {
                name = ((DataItemAttribute)obs[0]).Name;
            }

            if (String.IsNullOrEmpty(name))
            {
                name = t.Name;
            }

            switch (args.Change)
            {
            case ExtensionChange.Add:
                if (policyTypes.ContainsKey(t))
                {
                    throw new UserException("The Policy type '" + t.FullName + "' may only be registered once.");
                }
                if (policyNames.ContainsKey(name))
                {
                    throw new UserException("Only one Policy type may have the ID '" + name + "'");
                }
                policyTypes.Add(t, name);
                policyNames.Add(name, t);
                if (invariantPolicies.Get(t) == null)
                {
                    invariantPolicies.InternalSet(t, null, Activator.CreateInstance(t));
                }
                break;

            case ExtensionChange.Remove:
                policyTypes.Remove(t);
                policyNames.Remove(name);
                invariantPolicies.InternalRemove(t, null);
                break;
            }
        }