Skip to content

Commit d9fa7dc

Browse files
committed
Add some attributes
1 parent b1580fb commit d9fa7dc

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

Attributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Attribute
2424
/// <param name="value">The value of the Attribute, which must be of a supported Datamodel type.</param>
2525
public Attribute(string name, AttributeList owner, object value)
2626
{
27-
ArgumentNullException.ThrowIfNull(name);
27+
ArgumentNullException.ThrowIfNull(name);
2828

2929
Name = name;
3030
_Owner = owner;

Element.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ public class Element : AttributeList, INotifyPropertyChanged, ISupportInitialize
2929
/// <param name="name">An arbitrary string. Does not have to be unique, and can be null.</param>
3030
/// <param name="class_name">An arbitrary string which loosely defines the type of Element this is. Cannot be null.</param>
3131
/// <exception cref="IndexOutOfRangeException">Thrown when the owner already contains the maximum number of Elements allowed in a Datamodel.</exception>
32-
public Element(Datamodel owner, string name, Guid? id = null, string class_name = "DmElement")
32+
public Element(Datamodel owner, string name, Guid? id = null, string classNameOverride = null)
3333
: base(owner)
3434
{
3535
ArgumentNullException.ThrowIfNull(owner);
36-
ArgumentNullException.ThrowIfNull(class_name);
37-
36+
3837
Name = name;
39-
ClassName = class_name;
38+
ClassName = classNameOverride ?? GetType().Name;
4039

4140
if (id.HasValue)
4241
_ID = id.Value;

Format/Attribute.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace Datamodel.Format;
4+
5+
/// <summary>
6+
/// Subclass this attribute to define a custom attribute name convention.
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
9+
public abstract class AttributeNameConventionAttribute : System.Attribute
10+
{
11+
public abstract string GetAttributeName(string propertyName);
12+
}
13+
14+
public class AttributeNameLowercaseAttribute : AttributeNameConventionAttribute
15+
{
16+
public override string GetAttributeName(string propertyName)
17+
=> propertyName.ToLower();
18+
}
19+
20+
public class AttributeNameCamelCaseAttribute : AttributeNameConventionAttribute
21+
{
22+
public override string GetAttributeName(string propertyName)
23+
=> char.ToLowerInvariant(propertyName.AsSpan()[0]) + propertyName[1..];
24+
}
25+
26+
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
27+
public sealed class Attribute : System.Attribute
28+
{
29+
public Attribute(string name)
30+
{
31+
Name = name;
32+
}
33+
34+
public string Name { get; }
35+
}

0 commit comments

Comments
 (0)