-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using VariantEnum; | ||
|
||
namespace ConsoleApp; | ||
|
||
public enum IpAddrVariant : byte | ||
{ | ||
[VariantValueType(typeof(byte), typeof(byte), typeof(byte), typeof(byte))] | ||
V4, | ||
[VariantValueType(typeof(string))] | ||
V6 = 3, | ||
|
||
None, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
using ConsoleApp; | ||
using System.Buffers; | ||
using VariantEnum; | ||
|
||
Console.WriteLine("Hello, World!"); | ||
|
||
var v = new IpAddr.V4(127, 0, 0, 1); | ||
var v2 = new IpAddr.V6("::1"); | ||
|
||
public enum IpAddrVariant | ||
var v_string = $"IpAddr.V4 = {v}"; | ||
|
||
var str = (IpAddr)v switch | ||
{ | ||
[VariantValueType(typeof(byte), typeof(byte), typeof(byte), typeof(byte))] | ||
V41, | ||
[VariantValueType(typeof(string))] | ||
V61 | ||
IpAddr.V4 v4 => $"{v4.args0}.{v4.args1}.{v4.args2}.{v4.args3}", | ||
IpAddr.V6 v6 => v6.args0, | ||
_ => throw new Exception(), | ||
}; | ||
Console.WriteLine(str); | ||
Console.WriteLine(IpAddr.Count); | ||
|
||
var value = IpAddr.ConvertEnum(v); | ||
var value2 = IpAddr.ConvertEnum(v2); | ||
Console.WriteLine(IpAddr.GetName(v)); | ||
|
||
var buffer = new ArrayBufferWriter<char>(1024); | ||
if (v.TryFormat(buffer.GetSpan(1024), out var charsWritten)) | ||
{ | ||
buffer.Advance(charsWritten); | ||
var str2 = string.Create(charsWritten, (buffer, charsWritten), static (buffer, state) => | ||
{ | ||
for (var i = 0; i < state.charsWritten; i++) | ||
{ | ||
buffer[i] = state.buffer.WrittenSpan[i]; | ||
} | ||
}); | ||
Console.WriteLine(@$"TryFormat: {str2} {v}"); | ||
} | ||
|
||
Console.WriteLine("END"); |