NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
AbstractStyle.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Reflection;
12using System.Text;
14
16{
20 public abstract class AbstractStyle : IComparable<AbstractStyle>
21 {
25 [Append(Ignore = true)]
26 public int? InternalID { get; set; }
27
28
33 public abstract AbstractStyle Copy();
34
41 internal void CopyProperties<T>(T source, T reference) where T : AbstractStyle
42 {
43 if (source == null || GetType() != source.GetType() && GetType() != reference.GetType())
44 {
45 throw new StyleException("The objects of the source, target and reference for style appending are not of the same type");
46 }
47 PropertyInfo[] infos = GetType().GetProperties();
48 PropertyInfo sourceInfo;
49 PropertyInfo referenceInfo;
50 IEnumerable<AppendAttribute> attributes;
51 foreach (PropertyInfo info in infos)
52 {
53 attributes = info.GetCustomAttributes<AppendAttribute>();
54 if (attributes.Any() && !HandleProperties(attributes))
55 {
56 continue;
57 }
58 sourceInfo = source.GetType().GetProperty(info.Name);
59 referenceInfo = reference.GetType().GetProperty(info.Name);
60 if (!sourceInfo.GetValue(source).Equals(referenceInfo.GetValue(reference)))
61 {
62 info.SetValue(this, sourceInfo.GetValue(source));
63 }
64 }
65 }
66
72 private static bool HandleProperties(IEnumerable<AppendAttribute> attributes)
73 {
74 foreach (AppendAttribute attribute in attributes)
75 {
76 if (attribute.Ignore || attribute.NestedProperty)
77 {
78 return false; // skip property
79 }
80 }
81 return true;
82 }
83
89 public int CompareTo(AbstractStyle other)
90 {
91 if (!InternalID.HasValue)
92 {
93 return -1;
94 }
95 else if (other == null || !other.InternalID.HasValue)
96 {
97 return 1;
98 }
99 else
100 {
101 return InternalID.Value.CompareTo(other.InternalID.Value);
102 }
103 }
104
110 public bool Equals(AbstractStyle other)
111 {
112 if (other == null)
113 {
114 return false;
115 }
116 return this.GetHashCode() == other.GetHashCode();
117 }
118
126 internal static void AddPropertyAsJson(StringBuilder sb, string name, object value, bool terminate = false)
127 {
128 sb.Append('"').Append(name).Append("\": ");
129 if (value == null)
130 {
131 sb.Append("__null__");
132 }
133 else
134 {
135 sb.Append('"').Append(value.ToString().Replace("\"", "\\\"")).Append('"');
136 }
137 if (!terminate)
138 {
139 sb.Append(",\n");
140 }
141 }
142 }
143
144}
Class for exceptions regarding Style incidents.
Class represents an abstract style component.
int? InternalID
Gets or sets the internal ID for sorting purpose in the Excel style document (nullable).
int CompareTo(AbstractStyle other)
Method to compare two objects for sorting purpose.
bool Equals(AbstractStyle other)
Method to compare two objects for sorting purpose.
AbstractStyle Copy()
Abstract method to copy a component (dereferencing).
Attribute designated to control the copying of style properties.