RSCG – Monify
| name | Monify |
| nuget | https://www.nuget.org/packages/Monify/ |
| link | https://github.com/MooVC/monify |
| author | Paul Martin |
Generate primitive strongly typed wrapper around a single value object
This is how you can use Monify .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Monify" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information
using PrimitiveDemo;
Console.WriteLine("Hello, World!");
Person p= new ();
p.Age = 55;
Console.WriteLine($"Person's age is {p.Age}");
Console.WriteLine($"Is adult: {p.IsAdult}");
using System;
using System.Collections.Generic;
using System.Text;
namespace PrimitiveDemo;
[Monify.Monify<int>]
public partial struct Age;
internal class Person
{
public Age Age { get; set; }
public bool IsAdult => Age >= 18;
}
The code that is generated is
namespace Monify
{
using System;
using System.Diagnostics.CodeAnalysis;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
internal sealed class MonifyAttribute<T>
: Attribute
{
}
}
namespace Monify
{
using System;
using System.Diagnostics.CodeAnalysis;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
internal sealed class MonifyAttribute
: Attribute
{
private Type _type;
public Type Type
{
get
{
return _type;
}
set
{
_type = value;
}
}
}
}
namespace Monify.Internal
{
using System;
using System.Collections;
internal static class HashCode
{
private const int HashSeed = 0x1505;
private const int HashPrime = -1521134295;
public static int Combine(params object[] values)
{
int hash = HashSeed;
foreach (object value in values)
{
if (value is IEnumerable && !(value is string))
{
IEnumerable enumerable = (IEnumerable)value;
foreach (object element in enumerable)
{
hash = PerformCombine(hash, element);
}
}
else
{
hash = PerformCombine(hash, value);
}
}
return hash;
}
private static int PerformCombine(int hash, object value)
{
int other = GetHashCode(value);
unchecked
{
return (other * HashPrime) + hash;
}
}
private static int GetHashCode(object value)
{
int code = 0;
if (value != null)
{
code = value.GetHashCode();
}
return code;
}
}
}
namespace Monify.Internal
{
using System;
using System.Collections;
internal sealed class SequenceEqualityComparer
{
public static readonly SequenceEqualityComparer Default = new SequenceEqualityComparer();
public bool Equals(IEnumerable left, IEnumerable right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (ReferenceEquals(left, null) || ReferenceEquals(null, right))
{
return false;
}
return Equals(left.GetEnumerator(), right.GetEnumerator());
}
public int GetHashCode(IEnumerable enumerable)
{
return HashCode.Combine(enumerable);
}
private static bool Equals(IEnumerator left, IEnumerator right)
{
while (left.MoveNext())
{
if (!right.MoveNext())
{
return false;
}
if (!Equals(left.Current, right.Current))
{
return false;
}
}
return !right.MoveNext();
}
}
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static implicit operator int(Age subject)
{
if (ReferenceEquals(subject, null))
{
throw new ArgumentNullException("subject");
}
return subject._value;
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static implicit operator Age(int value)
{
return new Age(value);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public Age(int value)
{
_value = value;
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static bool operator ==(Age left, Age right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
{
return false;
}
return left.Equals(right);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static bool operator ==(Age left, int right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
{
return false;
}
return left.Equals(right);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public override bool Equals(object other)
{
if (other is Age)
{
return Equals((Age)other);
}
return false;
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public override int GetHashCode()
{
return global::Monify.Internal.HashCode.Combine(_value);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public bool Equals(Age other)
{
if (ReferenceEquals(this, other))
{
return true;
}
if (ReferenceEquals(other, null))
{
return false;
}
return Equals(other._value);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age : IEquatable<Age>
{
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public bool Equals(int other)
{
if (ReferenceEquals(this, other))
{
return true;
}
if (ReferenceEquals(other, null))
{
return false;
}
return global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(_value, other);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age : IEquatable<int>
{
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static bool operator !=(Age left, Age right)
{
return !(left == right);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public static bool operator !=(Age left, int right)
{
return !(left == right);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
public override string ToString()
{
return string.Format("Age {{ {0} }}", _value);
}
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
namespace PrimitiveDemo
{
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable disable
#endif
partial struct Age
{
private readonly int _value;
}
#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
#nullable restore
#endif
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Monify