-
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.
Implement generation attributes for enums and custom objects
- Loading branch information
Showing
8 changed files
with
115 additions
and
0 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
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
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,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using ChanceNET.Attributes; | ||
|
||
namespace ChanceNET.Tests | ||
{ | ||
public class Publisher | ||
{ | ||
[Age(AgeRanges.Teen)] | ||
public int Age; | ||
|
||
[FullName] | ||
public string Name; | ||
|
||
[Date] | ||
public DateTime Birthday; | ||
} | ||
} |
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
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,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ChanceNET.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public class EnumAttribute : ChanceAttribute | ||
{ | ||
Type enumType; | ||
|
||
public EnumAttribute(Type enumType) | ||
{ | ||
this.enumType = enumType; | ||
} | ||
|
||
internal override object GetValue(Chance chance) | ||
{ | ||
return Enum.ToObject(enumType, chance.PickEnum(enumType)); | ||
} | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ChanceNET.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public class ObjectAttribute : ChanceAttribute | ||
{ | ||
Type objectType; | ||
|
||
public ObjectAttribute(Type objectType) | ||
{ | ||
this.objectType = objectType; | ||
} | ||
|
||
internal override object GetValue(Chance chance) | ||
{ | ||
MethodInfo method = chance.GetType() | ||
.GetTypeInfo() | ||
.GetMethod("Object", new Type[] { }) | ||
.MakeGenericMethod(objectType); | ||
return method.Invoke(chance, null); | ||
} | ||
} | ||
} |
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
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