AOPMethods–adding partial functions and enums
I was finishing the AOPMethods – and what I have been thinking is – why not add partial functions ? I have added Console.Write, but … this seems more general… So , now , this is the Person class definition with the partial function definition
[AutoMethods(template = TemplateMethod.MethodWithPartial, MethodPrefix =”pub”, MethodSuffix =”bup”)]
partial class Person
{
partial void Method_Start(string methodName)
{
Console.WriteLine($”start {methodName}”);
}
partial void Method_End(string methodName)
{
Console.WriteLine($”end {methodName}”);
}
And, because I have finished the AOPMethods , I have been thinking about the problems with enums – every time I parse the enum. What about a autogenerated function ? So now , with this definition:
[AutoEnum(template = EnumMethod.GenerateExtensionCode)]
/// <summary>
/// my test
/// </summary>
public enum Test:long
{
a,
//the b should be 1
b,
/// <summary>
/// x should be 2
/// </summary>
x=5,
y=7}
I can do this
long y = 7;
var s = y.ParseExactTest2();
var q=y.ToString().ParseExactTest2();
var s1 = s.ToString().ParseExactTest2();
For reference, the generated code with Roslyn AOPMethods is:
public static AOPMethodsTest.Test2 ParseExactTest2(this long value, AOPMethodsTest.Test2? defaultValue = null)
{
if (0 == value)
return AOPMethodsTest.Test2.a1;
if (1 == value)
return AOPMethodsTest.Test2.b1;
if (5 == value)
return AOPMethodsTest.Test2.x1;
if (7 == value)
return AOPMethodsTest.Test2.y1;if (defaultValue != null)
return defaultValue.Value;throw new ArgumentException(“cannot find ” + value + ” for AOPMethodsTest.Test2 “);
}public static AOPMethodsTest.Test2 ParseExactTest2(this string value, AOPMethodsTest.Test2? defaultValue = null)
{
//trying to see if it is a value inside
//if(!string.IsNullOrWhiteSpace)
if (long.TryParse(value, out long valueParsed))
{
return ParseExactTest2(valueParsed);
}if (0 == string.Compare(“a1”, value, StringComparison.InvariantCultureIgnoreCase))
return AOPMethodsTest.Test2.a1;
if (0 == string.Compare(“b1”, value, StringComparison.InvariantCultureIgnoreCase))
return AOPMethodsTest.Test2.b1;
if (0 == string.Compare(“x1”, value, StringComparison.InvariantCultureIgnoreCase))
return AOPMethodsTest.Test2.x1;
if (0 == string.Compare(“y1”, value, StringComparison.InvariantCultureIgnoreCase))
return AOPMethodsTest.Test2.y1;if (defaultValue != null)
return defaultValue.Value;throw new ArgumentException(“cannot find ” + value + ” for AOPMethodsTest.Test2 “);
}
Leave a Reply