示例#1
0
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (reader == null) throw new ArgumentNullException(nameof(reader));

            Id = Guid.Parse(reader.GetAttribute(nameof(Id)) ??
                            throw new InvalidOperationException("This license is invalid."));

            ProductName = reader.GetAttribute(nameof(ProductName)) ??
                          throw new InvalidOperationException("This license is invalid.");

            ProductVersion = reader.GetAttribute(nameof(ProductVersion)) ??
                             throw new InvalidOperationException("This license is invalid.");

            CustomerName = reader.GetAttribute(nameof(CustomerName)) ??
                           throw new InvalidOperationException("This license is invalid.");

            SerialNumber = reader.GetAttribute(nameof(SerialNumber)) ??
                           throw new InvalidOperationException("This license is invalid.");

            CreationTime = DateTime.Parse(reader.GetAttribute(nameof(CreationTime)) ??
                                          throw new InvalidOperationException("This license is invalid."));

            ExpirationTime = ExpirationTime.Parse(reader.GetAttribute(nameof(ExpirationTime)) ??
                                                  throw new InvalidOperationException("This license is invalid."));

            while (reader.Read())
            {
                if (reader.NodeType != XmlNodeType.Element) continue;

                switch (reader.Name)
                {
                    case "Feature":
                    {
                        var feature = LicenseFeature.New(reader.GetAttribute(nameof(LicenseFeature.Name)),
                            reader.GetAttribute(nameof(LicenseFeature.DisplayName)),
                            reader.GetAttribute(nameof(LicenseFeature.Value)),
                            reader.GetAttribute(nameof(LicenseFeature.Description)));
                        var result = AddFeature(feature);
                        if (result.Failed) throw new InvalidOperationException(result.Message);
                        break;
                    }

                    case "Attribute":
                    {
                        var name = reader.GetAttribute("Name");
                        reader.Read();
                        var value = reader.Value;
                        var result = AddAttribute(name, value);
                        if (result.Failed) throw new InvalidOperationException(result.Message);
                        break;
                    }
                }
            }
        }
示例#2
0
        public Result AddFeature(LicenseFeature feature)
        {
            if (feature == null) throw new ArgumentNullException(nameof(feature));

            if (Signed) throw new InvalidOperationException("This license already is signed.");

            if (_features.Contains(feature)) return Result.Fail($"A feature with name {feature.Name} already exists.");

            _features.Add(feature);

            return Result.Ok();
        }