Im working on a console tool which accepts some arguments and then parses to the Option class.
What I need is a property that will verify if only one of many marked fields in Option class has values (arguments were delivered).
Ex.
Its ok when we run:
my.exe -a
my.exe -b
but NOT:
my.exe
my.exe -a -b
CommandLine.OptionAttribute cannot do such a thing. What i did is:
Main class args[] got extension .Parse:
args.Parse(options)` //where options is my Options class
inside:
CommandLine.Parser.Default.ParseArguments(args, options);
var isOnlyOneOfMany = options.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(OneOfMany)) && prop.GetValue(options) != null).Count() == 1;
how to do this better way?
I will rewrite your Options class
class Options
{
public int OneOfManyCount { get; private set;}
// This is not OneOfMany
[Option('n', "name", Required = true)]
public string Name { get; set; }
private string _Value;
[OneOfMany]
[Option('v', "value", Required = true)]
public string Value { get { return _Value; } set { _Value = value; OneOfManyCount++;} }
private string _Date;
[OneOfMany]
[Option('d', "data", Required = true)]
public string Data { get { return _Date; } set { _Date = value; OneOfManyCount++;} }
}
and in your main, you can call options.OneOfManyCount to get the number of arguments
CommandLine.Parser.Default.ParseArguments(args, options);
if(options.OneOfManyCount != 1) //Do something
And please notice if you have a DefaultValue attribute on one of you OneOfMany, it will hit the set one more time which means OneOfManyCount will have unexpected value.
Update: This solution doesn't work from version 2.8.0 because both SetName and Group are not allowed in option
You can use SetName and GroupName to achieve this behavior:
GroupName: Available from version 2.7+
An option group represents a group of options which are optional, but at least one should be available.
SetName: Available from version 1.9+
It means that you can run commands of one set at a time. You can't mix
commands of more than one set otherwise you get an error.
The new option class:
public class Options
{
[Option('a', "aValue", Group = "Values", SetName = "ASet")]
public bool AValue { get; set; }
[Option('b', "aValue", Group = "Values", SetName = "BSet")]
public bool BValue { get; set; }
}
How to parse the args:
var options = Parser.Default.ParseArguments<Options>(args);
Related
Say we work with this class:
public class UsefulClass
{
public string A { get; set; }
public string B { get; set; }
public int? C { get; set; }
public int? D { get; set; }
public decimal E { get; set; }
public decimal F { get; set; }
}
Let's consider the following instance:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
At this point, z_objUsefulInstance.A and C are null, E is 0, B, D and F have not been initialized.
Is there a way to tell, automatically, which properties of z_objUsefulInstance haven't been initialized and which ones have been initialized with null or 0?
EDIT: by popular demand, why I need this: to emulate a system of database access akin to EntityFramework. Right now all properties are a specific generic type, so it's rather easy to know which is null and which is Generic<T>.HasNullValue == true. But that generic type causes various issues and now we'd like to get rid of it, particularly as we have grown more conversant with Expressions.
Is there a way to tell, automatically, which properties of z_objUsefulInstance haven't been initialized and which ones have been initialized with null or 0?
You can't really know in ways that you can easily inspect at runtime what properties have been set unless you intercept the property setter and set some sort of flag. from a first-principals perspective that would resemble something like this:
public class UsefulClass
{
public string A { get => _a; set { _a = value; A_Set = true; } }
private string _a;
private bool A_Set = false;
public string B { get => _b; set { _b = value; B_Set = true; } }
private string _b;
private bool B_Set = false;
public int? C { get => _c; set { _c = value; C_Set = true; } }
private string _c;
private bool C_Set = false;
public int? D { get => _d; set { _d = value; D_Set = true; } }
private string _d;
private bool D_Set = false;
public decimal E { get => _e; set { _e = value; E_Set = true; } }
private string _e;
private bool E_Set = false;
public decimal F { get => _f; set { _f = value; F_Set = true; } }
private string _f;
private bool F_Set = false;
}
It is pretty verbose, but you can see here how we are not comparing the value at all, we can determine definitively if each property has been set, thought not specifically during the initialization of the instance, this simple code only tracks if each property was set at all.
So after your init, we can inspect these new flags:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
Console.WriteLine(z.C_Set); // True
Console.WriteLine(z.D_Set); // False
We can simplify this with a dictionary for the backing store and helper methods to get and set the property values, we can even encapsulate that logic in a base class to make this easier to consume:
public class UsefulClass : PropertyTracker
{
public string A { get => GetProperty<string>(); set => SetProperty(value); }
public string B { get => GetProperty<string>(); set => SetProperty(value); }
public int? C { get => GetProperty<int?>(); set => SetProperty(value); }
public int? D { get => GetProperty<int?>(); set => SetProperty(value); }
public decimal E { get => GetProperty<decimal>(); set => SetProperty(value); }
public decimal F { get => GetProperty<decimal>(); set => SetProperty(value); }
}
public abstract class PropertyTracker
{
private Dictionary<string, object> _values = new Dictionary<string, object>();
protected void SetProperty<T>(T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
_values[propertyName] = value;
}
protected T GetProperty<T>([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null)
{
if (!_values.ContainsKey(propertyName))
return default;
return (T)_values[propertyName];
}
public bool IsSet(string propertyName)
{
return _values.ContainsKey(propertyName);
}
}
See we still have the concept of a backing store, it's just not a field anymore. The inspection code is a bit different too:
UsefulClass z_objUsefulInstance = new UsefulClass()
{
A = null,
C = null,
E = 0
};
Console.WriteLine(z.IsSet(nameof(UsefulClass.C)); // True
Console.WriteLine(z.IsSet(nameof(UsefulClass.D)); // False
There are all sorts of techniques you can use to scaffold this or similar code out across your classes, this is just an example implementation. You could even write a generic wrapper that uses reflection to do the same thing. In my solutions I tend to use T4 templates to generate what are effectively View Model classes. My main argument was that I could generate some verbose code and take a hit at compile-time instead of a performance hit at runtime with a reflection based implementation.
If your ViewModel classes inherit from your model class, then you can get close to an apparently automatic implementation that is more compatible with the rest of your runtime, but that would require your properties be declared as virtual to enable the inheriting class to override the implementation.
If you end up going down this route, consider adding value to your classes by implementing INotifyPropertyChanged, or perhaps IChangeTracking or IRevertibleChangeTracking while you're there.
UsefulClass z_objUsefulInstance = new UsefulClass() {
A = null
C = null,
E = 0
};
At this point, z_objUsefulInstance.A and C are null, E is 0,
B, D and F have not been initialized.
No that's not quite right.
From "14.11.4 Constructor Execution" in the C#7 language spec
Variable initializers are transformed into assignment statements, and these assignment statements are executed before the invocation of the base class instance constructor.
So before your instance constructor in the above example is started executing, the properties are assigned
A = default(string); // null
B = default(string); // null
C = default(int?); // null
D = default(int?); // null
E = default(decimal); // 0.0m
F = default(decimal); // 0.0m
(Not quite accurate, but close enough for this answer)
Then your instance constructor is run (in this example, the default provided by the compiler), then your property assignments are made
A = null,
C = null,
E = 0
. There's no difference between E = 0 and E = default(decimal), nor is there a difference between null and null (default(string)).
If you need to tell whether a property was set or not you will have to provide a backing field, or otherwise control access to the property.
If you want to read more about constructor details, a friendlier summary than the language spec can be found at https://jonskeet.uk/csharp/constructors.html .
I have below method on service layer which will be called from different controller with different actionType(parameter to my method).
public async Task<OutPutList[]> GetMyDataAsync(string actionType)
{
var request = new getMyDataRequest
{
getMyDatas = new getMyDatas
{
AFlag= false,
AFlagSpecified = false,
BFlag=false,
BFlagSpecified=false,
cFlag=false,
cFlagSpecified=false,
commonproperty= ""
}
};
return await _myService.method(request);
}
If the actionType is specified as "A" , the AFlag and AFlagSpecified properties value to be set as "True" . And if i specify actionType as B then BFlag and BFlagSpecified values to be set as true and AFlag will be false.how this can be done dynamically or in any smpler way? I am having around 12 actiontypes and if i create different request object then i need to write 12 if else condition. Can this be simplified with minimum code.
You can use Reflection to make your code dynamic.
To make you understand more, You can look below code
getMyDatas.cs
public class getMyDatas
{
public bool AFlag { get; set; }
public bool BFlag { get; set; }
public bool CFlag { get; set; }
}
Program.cs
using StackOverFlowTest;
using System;
using System.Reflection;
//input
string actionType = "A";
//Business
var Data = new getMyDatas();
PropertyInfo[] properties = typeof(getMyDatas).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(Data, Convert.ChangeType(property.Name.StartsWith(actionType), property.PropertyType), null);
}
//Testing
Console.WriteLine(Data.AFlag);
Console.WriteLine(Data.BFlag);
Console.WriteLine(Data.CFlag);
Shortly, I check if property name starts with input
I may not have a good grasp of the ?? operator yet and ran into a design flaw I couldn't explain.
Compare the following two properties, the only difference being how there are initialized: the first explicitly initialized, while the second with the ?? operator (or am I doing it wrong here?).
If I run data init with both properties, the collection based on the first property comes up populated as expected, while the second one with the ?? operator never gets populated and comes up with 0 elements in the collection.
Surely something is wrong here in my assumption; what is the flaw here?
P.S. Please ignore the Set method which is to implement INotifyPropertyChanged in the base class and has no bearing on this issue (which is confined to the type of initialization).
// property version 1
private ObservableCollection<UserName> _userNameColl = new ObservableCollection<UserName>();
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl; }
set { Set(ref _userNameColl, value); }
}
// property version 2
private ObservableCollection<UserName> _userNameColl;
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl ?? new ObservableCollection<UserName>(); }
set { Set(ref _userNameColl, value); }
}
// a simple class for creating object collection
public class UserName
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
// a simple test populating the collection
for (int i = 0; i < 4; i++)
{
// silly data init just for test
UserNameColl.Add(new UserName()
{
Name = $"UserName No {i}",
Age = 20 + i,
Email = $"email{i}#local.lan"
});
}
The second one never initializes your field but always returns a new collection. Try this one instead:
public ObservableCollection<UserName> UserNameColl
{
get { return _userNameColl ?? (_userNameColl = new ObservableCollection<UserName>()); }
set { Set(ref _userNameColl, value); }
}
I have created a class that contains two variables: Type & Value. If the first property (Type) is filled, the second property (Value) can only contain a value that matches the type which is selected on the Type property.
public class Requirement
{
public RequirementType Type { get; set; }
public object Value { get; set; }
public enum RequirementType
{
OS, NetFramework, Connection
}
public enum OSType
{
// Used for RequirementType.OS
Win, Unix, MacOSX
}
public enum NetFrameworkType
{
// Used for RequirementType.NetFramework
Two, Three, Four, FourHalf
}
public enum ConnectionType
{
// Used for RequirementType.Connection
Internet, Connected, None
}
}
I'm using this class in the XAML:
<util:Requirement Type="OS" Value="Win" />
So for example, if the enum value OS has been chosen. The only valid values should be from the enum OSType. I started looking in the .Net source how they solved this with the System.Windows.Trigger & System.Windows.Setter but no success yet.. It seems to be something with the DependsOn attribute and XamlSetTypeConverterAttribute. Does someone know the solution to this problem?
You can use a backing field for value and check each type as it's being set.
public class Requirement
{
public RequirementType Type { get; set; }
private object _value;
public object Value
{
get { return _value; }
set
{
if (Type == RequirementType.OS &&
value.GetType() == typeof(OSType))
{
_value = value;
}
else
{
throw new Exception("Value type is incorrect for Type provided");
}
}
}
}
This test will throw the exception:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = RequirementType.Connection;
While this second test will properly set the value:
var req = new Requirement();
req.Type = RequirementType.OS;
req.Value = OSType.Win;
You can use normal properties (type propful and hit Tab):
private RequirementType _type;
public RequirementType Type
{
get { return _type; }
set
{
_type = value;
// do whatever logic you want here
}
}
I'm trying to write some activities with C# instead of the designer and XAML. VS2010 has been buggy and very slow for that, and it also has very poor compilation support (for variables names, properties and so on).
So I'm trying to create activities by inheriting from the Activity class directly, but I'm encountering a snag.
Here's my code:
public class TestActivity : Activity
{
public InArgument<string> Username { get; set; }
public InArgument<string> Password { get; set; }
public OutArgument<bool> ValidCredential { get; set; }
public OutArgument<ProvisioningRole> Role { get; set; }
public OutArgument<Guid> Guid { get; set; }
protected override Func<Activity> Implementation
{
get
{
return () =>
{
return new Sequence()
{
Activities =
{
new AuthenticateUserActivity()
{
Username = this.Username,
Password = this.Password,
Guid = this.Guid,
Result = this.ValidCredential
},
new If()
{
Condition = this.ValidCredential,
Then = new GetUserRoleActivity()
{
Username = this.Username,
Password = this.Password,
Result = this.Role
}
},
}
};
};
}
set { base.Implementation = value; }
}
}
The problem is with the If(), the condition. It's supposed to be an InArgument, but this.ValidCredential is an OutArgument. I've tried creating a Variable, assign the value of ValidCredential to it. I also tried to put the result of AuthenticateUserActivity in the variable and then assign it to ValidCredential, but I get an error saying the To property of Assign needs to be specified.
I've looked around for proper tutorials, but all I found was an MSDN article that had a quick and dirty code implementation, and it used literals instead of the passed arguments, so no help from there.
I found out how to do it. You just need to create new InArgument from the original one. There is a constructor that takes an expression for it.
Username = new InArgument<bool>((ActivityContext c) => this.ValidCredential.Get(c))
So I changed my whole activity to
return new CompensableActivity()
{
Body = new Sequence()
{
Activities =
{
new AuthenticateUserActivity()
{
Username = this.Username.In(),
Password = this.Password.In(),
Guid = this.Guid.Out(),
Result = this.ValidCredential.Out()
},
new If(this.ValidCredential.In())
{
Then = new GetUserRoleActivity()
{
Username = this.Username.In(),
Password = this.Password.In(),
Result = this.Role.Out()
},
Else = new Assign<ProvisioningRole>()
{
To = this.Role.Out(),
Value = ProvisioningRole.User
}
}
}
},
};
In and Out being extension methods I wrote:
public static class WorkflowExtensions
{
#region In
public static InArgument<T> In<T>(this InArgument<T> self)
{
return new InArgument<T>(context => self.Get(context));
}
public static InArgument<T> In<T>(this OutArgument<T> self)
{
return new InArgument<T>(context => self.Get(context));
}
#endregion
#region Out
public static OutArgument<T> Out<T>(this InArgument<T> self)
{
return new OutArgument<T>(context => self.Get(context));
}
public static OutArgument<T> Out<T>(this OutArgument<T> self)
{
return new OutArgument<T>(context => self.Get(context));
}
#endregion
}
And now all is well!
You should be able to get this to work. The basic approach should be to use a Variable to store data, use an OutArgument to get data out of activities into the Variable and InArguments to get data from a Variable into an activity.
Also note that the expressions to tie InArguments to Variables are VisualBasicValue expressions. So something like:
Condition = new VisualBasicValue("System.DateTime.Now.Hour < 12")
This blog post isn't about using arguments and variables but shows a couple of examples.
Going to shamelessly plug my own library that I ended up making for this:
http://code.google.com/p/system-transactions/
Allows basic compensation of code without the ginormous hassle of WF. Also, compiles properly and is easily debuggable.