I want to be able to:
Check if an object has an indexing operator defined.
If it is defined, I want to be able to use it.
I want to implement this in the following code.
The code contains an object (MyObject) that offers a way to traverse through a multidimensional array or linked set of hash tables. Also it should prevent giving errors if a node in the requested path doesn't exist.
The part that I can't figure out is the commented part in the code:
public class MyObject
{
private object myObject = null;
public MyObject()
{
}
public MyObject(object value)
{
myObject = value;
}
public void setValue(object value)
{
myObject = value;
}
public object getValue()
{
return myObject;
}
public object this[string key]
{
get
{
if (myObject == null)
{
return new MyObject(null);
}
else
{
// determine what of type/class myObject is and if it has indexing operators defined
// if defined, access them and return the result
// else return null.
}
}
set
{
if (myObject == null)
{
// do nothing (or throw an exception);
}
else{
// determine what of type/class myObject is
// determine if that type/class has indexing operators defined
// if defined, access them and set the result there
// else do nothing (or throw an exception).
}
}
}
}
This is what I wish to accomplish:
// given these variables:
string loremIpsumString = "lorem ipsum dolor sit amet";
int[] digits = new int[10];
for (int i = 0; i <= 3; i++) digits[i] = i;
Hashtable outerHashtable = new Hashtable();
Hashtable innerHashtable = new Hashtable();
innerHashtable.Add("contents", "this is inside");
outerHashtable.Add("outside", "this is outside");
outerHashtable.Add("inside", innerHashtable);
// I can already print this:
Response.Write( loremIpsumString ); // prints "lorem ipsum dolor sit amet"
Response.Write( digits[0] ); // prints "0"
Response.Write( digits[1] ); // prints "1"
Response.Write( digits[2] ); // prints "2"
Response.Write( outerHashtable["outside"] ); // prints "this is outside"
Response.Write( ((Hashtable)outerHashtable["inside"])["contents"] ); // prints "this is outside"
// But I want to be to do it this way:
MyObject myObject;
myObject = new MyObject(loremIpsumString);
Response.Write( myObject.getValue() ); // prints "lorem ipsum dolor sit amet"
Response.Write( myObject["unexistant"].getValue() ); // prints nothing/null
myObject = new MyObject(digits);
Response.Write( myObject[0].getValue() ); // prints "0"
Response.Write( myObject[1].getValue() ); // prints "1"
Response.Write( myObject[2].getValue() ); // prints "2"
myObject = new MyObject(outerHashtable);
Response.Write( myObject["outside"].getValue() ); // prints "this is outside"
Response.Write( myObject["inside"]["contents"].getValue() ); // prints "this is inside"
Response.Write( myObject["unexistant"].getValue() ); // prints nothing/null
Response.Write( myObject["unexistant"]["unexistant"]["unexistant"].getValue() ); // prints nothing/null
You can first check if it's inheriting IList to cover (generic) Lists and Arrays. If not you can use PropertyInfo.GetIndexParameters to check if it has an indexer instead:
get
{
if (myObject == null)
{
return null;
}
else
{
// not sure which index(es) you want
int index = 0;
Type t = myObject.GetType();
if (typeof(IList).IsAssignableFrom(t))
{
IList ilist = (IList)myObject;
return ilist[index];
}
else
{
var indexer = t.GetProperties()
.Where(p => p.GetIndexParameters().Length != 0)
.FirstOrDefault();
if (indexer != null)
{
object[] indexArgs = { index };
return indexer.GetValue(myObject, indexArgs);
}
else
return null;
}
}
}
DEMO (with a string which has an indexer to access the chars)
You can test if the object is a dictionary
public object this[string key]
{
get
{
var dict = myObject as IDictionary;
if (dict == null) {
return null;
}
if (dict.Contains(key)) {
return dict[key];
}
return null;
}
set
{
var dict = myObject as IDictionary;
if (dict != null) {
dict[key] = value;
}
}
}
Note: If you have the control over the dictionary type to use, then prefer Dictionary<string,object> over Hashtable. Its handy method TryGetValue allows you to safely access it without first calling Contains and thus saves you from accessing it twice. Of cause you would then cast to Dictionary<string,object> instead of IDictionary.
var dict = myObject as Dictionary<string,object>;
if (dict == null) {
return null;
}
object result;
dict.TryGetValue(key, out result); // Automatically sets result to null
// if an item with this key was not found.
return result;
For others looking for the answer. Here is what I made of it with #TimSchmelter 's help.
So this is the code that I implemented in the get{} that I used in the code at the top of this screen, where in the top of this screen the get{} just contained comments.
get
{
if (myObject == null)
return new MyObject(null);
object returnValue = null;
bool foundReturnValue = false;
object[] indexArgs = { key };
Type myObjectType = myObject.GetType();
if (typeof(IList).IsAssignableFrom(myObjectType))
{
try
{
returnValue = ((IList)myObject)[((int)key)];
foundReturnValue = true;
}
catch (Exception) { }
}
if (!foundReturnValue)
{
foreach (PropertyInfo property in myObjectType.GetProperties())
{
ParameterInfo[] indexParameters = property.GetIndexParameters();
foreach (ParameterInfo indexParameter in indexParameters)
{
if (indexParameter.ParameterType.IsAssignableFrom(key.GetType()))
{
try
{
returnValue = property.GetValue(myObject, indexArgs);
foundReturnValue = true;
}
catch (Exception) { }
}
if (foundReturnValue == true)
break;
}
if (foundReturnValue == true)
break;
}
}
return new MyObject(returnValue);
}
Related
Storing the generic class T in a variable and reusing it in sub methode.
For a WebService with crud on few object:
Foo: Bar: Etc..
SetFoo SetBar SetEtc
GetFoo GetBar GetEtc
UpdateFoo UpdateBar UpdateEtc
DeleteFoo DeleteBar DeleteEtc
GetList .. ..
GetPending .. ..
Processed .. ..
I have the following singleton generic wrapper on client side, with methode like:
public bool Get<T>(int i, out DloExtention result)
// DloExtention is an interface implemented by foo, bar, etc..
{
result = null;
try
{
if (typeof(T) == typeof(Foo))
{
result = WebserviceClient.GetFoo(i);
}
else if (typeof(T) == typeof(Bar))
{
result = WebserviceClient.GetBar(i);
}
else if (typeof(T) == typeof(Etc))
{
result = WebserviceClient.GetEtc(i);
}
else
{
throw new NotSupportedException("Get<T>, T is not a supported type.");
}
}
catch (Exception ex)
{
Log4N.Logger.Error($"Error in Namespace.ClientSide.Get<{nameof(T)}>(int {i} ). " + ex.Message);
return false;
}
return true;
}
So I can handle all the type simply with the same generic object:
class Processor
{
HashSet<int> validOperation = new HashSet<int>();
HashSet<int> invalidOperation = new HashSet<int>();
internal void Run<T>()
{
if (Wrapper.Instance.GetListPending<T>(out int[] newEntityList) && newEntityList.Any())
{
ProcessEntities<T>(newEntityList, false);
}
}
private void ProcessEntities<T>(int[] idsEnt, bool singleMode)
{
foreach (var idEnt in idsEnt)
{
ProcessEntity<T>(idEnt, false);
}
CloseValidOperation();
RemoveInvalidOperation();
}
internal void ProcessIncident<T>(int idEnt)
{
if (Wrapper.Instance.Get<T>(idEnt, out LanDataExchangeCore.LanDataExchangeWCF.DloExtention currentEntity))
{
if (currentEntity.isValid() && currentEntity.toLocalDB())
{
validOperation.Add(idEnt);
}
else
{
invalidOperation.Add(idEnt);
}
}
}
Only Wrapper.Instance.Get<T> and Wrapper.Instance.GetListPending<T> needs the generic parameter.
But every methode in the way need to use it only to be able to deliver <T> to the last methode.
Is there a way to save the <T> in the Run<T> call into a private variable so inner methode of the class can use it ?
I have try adding a Type myType; but can't find the way to use it in generic call. Exemple for the Wrapper.Instance.Get<T>
Type myType; // class property
var fooWrapperGet = typeof(Wrapper).GetMethod("Get");
var fooOfMyTypeMethod = fooWrapperGet.MakeGenericMethod(new[] { myType });
//fooOfMyTypeMethod.Invoke(Wrapper.Instance , new object[] { new myType() });
// fooWrapperGet, as my wrapper is a singleton, Wrapper dont exposed Get<T>, but Wrapper.instance will expose it.
// new myType() <- do not compile.
Does this work for you?
private Dictionary<Type, Func<int, DloExtention>> gets =
new Dictionary<System.Type, Func<int, DloExtention>>()
{
{ typeof(Foo), WebserviceClient.GetFoo },
{ typeof(Bar), WebserviceClient.GetBar },
{ typeof(Etc), WebserviceClient.GetEtc },
};
public bool Get<T>(int i, out DloExtention result)
{
result = null;
var flag = false;
if (gets.ContainsKey(typeof(T)))
{
result = gets[typeof(T)](i);
flag = true;
}
return flag;
}
The beauty of this is that you can populate your dictionary at run-time.
I have a GetDynamicParameters() on cmdlet Get-DateSlain that does something like this:
public object GetDynamicParameters()
{
List<string> houseList = {"Stark", "Lannister", "Tully"};
var attributes = new Collection<Attribute>
{
new ParameterAttribute
{
HelpMessage = "Enter a house name",
},
new ValidateSetAttribute(houseList.ToArray()),
};
if (!this.ContainsKey("House"))
{
this.runtimeParameters.Add("House", new RuntimeDefinedParameter("House", typeof(string), attributes));
}
}
And this works as expected - users can type Get-DateSlain -House, and tab through the available houses. However, once a house is chosen, I want to be able to narrow down the results to characters in that house. Furthermore, if it's house 'Stark', I want to allow a -Wolf parameter. So to implement (some value validity checks removed for brevity):
public object GetDynamicParameters()
{
if (this.runtimeParameters.ContainsKey("House"))
{
// We already have this key - no need to re-add. However, now we can add other parameters
var house = this.runtimeParameters["House"].Value.ToString();
if (house == "Stark")
{
List<string> characters = { "Ned", "Arya", "Rob" };
var attributes = new Collection<Attribute>
{
new ParameterAttribute
{
HelpMessage = "Enter a character name",
},
new ValidateSetAttribute(characters.ToArray()),
};
this.runtimeParameters.Add("Character", new RuntimeDefinedParameter("Character", typeof(string), attributes));
List<string> wolves = { "Shaggydog", "Snow", "Lady" };
var attributes = new Collection<Attribute>
{
new ParameterAttribute
{
HelpMessage = "Enter a wolf name",
},
new ValidateSetAttribute(wolves.ToArray()),
};
this.runtimeParameters.Add("Wolf", new RuntimeDefinedParameter("Wolf", typeof(string), attributes));
}
else if (house == "Lannister")
{
List<string> characters = { "Jaimie", "Cersei", "Tywin" };
// ...
}
// ...
return this.runtimeParameters;
}
List<string> houseList = {"Stark", "Lannister", "Tully"};
var attributes = new Collection<Attribute>
{
new ParameterAttribute
{
HelpMessage = "Enter a house name",
},
new ValidateSetAttribute(houseList.ToArray()),
};
this.runtimeParameters.Add("House", new RuntimeDefinedParameter("House", typeof(string), attributes));
}
This looks like it should work, but it doesn't. The GetDynamicParameters function is only called once, and that is before a value is supplied to this.runtimeParameters["House"]. Since it doesn't re-evaluate after that value is filled in, the additional field(s) are never added, and any logic in ProcessRecord that relies on these fields will fail.
So - is there a way to have multiple dynamic parameters that rely on each other?
Have a look a the aswer to this question, it shows a way to access the values of other dynamic parameters in the GetDynamicParameters method:
Powershell module: Dynamic mandatory hierarchical parameters
I adapted the code from the mentioned answer so it can handle SwitchParameters and the raw input parameter is converted to the actual type of the cmdlet parameter. It does not work if the dynamic parameter you want to get the value for is passed via pipeline. I think that is not possible because dynamic parameters are always created before pipeline input is evaluated. Here it is:
public static class DynamicParameterExtension
{
public static T GetUnboundValue<T>(this PSCmdlet cmdlet, string paramName, int unnamedPosition = -1))
{
var context = TryGetProperty(cmdlet, "Context");
var processor = TryGetProperty(context, "CurrentCommandProcessor");
var parameterBinder = TryGetProperty(processor, "CmdletParameterBinderController");
var args = TryGetProperty(parameterBinder, "UnboundArguments") as System.Collections.IEnumerable;
if (args != null)
{
var isSwitch = typeof(SwitchParameter) == typeof(T);
var currentParameterName = string.Empty;
object unnamedValue = null;
var i = 0;
foreach (var arg in args)
{
var isParameterName = TryGetProperty(arg, "ParameterNameSpecified");
if (isParameterName != null && true.Equals(isParameterName))
{
var parameterName = TryGetProperty(arg, "ParameterName") as string;
currentParameterName = parameterName;
if (isSwitch && string.Equals(currentParameterName, paramName, StringComparison.OrdinalIgnoreCase))
{
return (T)(object)new SwitchParameter(true);
}
continue;
}
var parameterValue = TryGetProperty(arg, "ArgumentValue");
if (currentParameterName != string.Empty)
{
if (string.Equals(currentParameterName, paramName, StringComparison.OrdinalIgnoreCase))
{
return ConvertParameter<T>(parameterValue);
}
}
else if (i++ == unnamedPosition)
{
unnamedValue = parameterValue;
}
currentParameterName = string.Empty;
}
if (unnamedValue != null)
{
return ConvertParameter<T>(unnamedValue);
}
}
return default(T);
}
static T ConvertParameter<T>(this object value)
{
if (value == null || Equals(value, default(T)))
{
return default(T);
}
var psObject = value as PSObject;
if (psObject != null)
{
return psObject.BaseObject.ConvertParameter<T>();
}
if (value is T)
{
return (T)value;
}
var constructorInfo = typeof(T).GetConstructor(new[] { value.GetType() });
if (constructorInfo != null)
{
return (T)constructorInfo.Invoke(new[] { value });
}
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch (Exception)
{
return default(T);
}
}
static object TryGetProperty(object instance, string fieldName)
{
if (instance == null || string.IsNullOrEmpty(fieldName))
{
return null;
}
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
var propertyInfo = instance.GetType().GetProperty(fieldName, bindingFlags);
try
{
if (propertyInfo != null)
{
return propertyInfo.GetValue(instance, null);
}
var fieldInfo = instance.GetType().GetField(fieldName, bindingFlags);
return fieldInfo?.GetValue(instance);
}
catch (Exception)
{
return null;
}
}
}
So for your example you should be able to use it like:
public object GetDynamicParameters()
{
var houseList = new List<string> { "Stark", "Lannister", "Tully" };
var attributes = new Collection<Attribute>
{
new ParameterAttribute { HelpMessage = "Enter a house name" },
new ValidateSetAttribute(houseList.ToArray()),
};
var runtimeParameters = new RuntimeDefinedParameterDictionary
{
{"House", new RuntimeDefinedParameter("House", typeof (string), attributes)}
};
var selectedHouse = this.GetUnboundValue<string>("House");
//... add parameters dependant on value of selectedHouse
return runtimeParameters;
}
After all I'm not sure if it's a good idea trying to get those dynamic parameter values in the first place. It is obviously not supported by PowerShell Cmdlet API (see all the reflection to access private members in the GetUnboundValue method), you have to reimplement the PowerShell parameter conversion magic (see ConvertParameter, I'm sure I missed some cases there) and there is the restriction with pipelined values. Usage at your own risk:)
I have the problem, that i don't know how to call the this property of an object instance over reflection.
I have the following method:
public object GetValue(SOP sop, object value)
{
// 1. find the this getter/setter method for the value parameter
// 2. Invoke the method and pass the result of GetIndexArray(sop) as a parameter
// 3. return the result of the method
}
which should call the this property of the value-instance and return the value the property returns.
The problem is, that i don't know the type of the value-instance, which is passed as a parameter.
The index which should be passed to the this property is given over the following method:
private object[] GetIndexArray(SOP sop)
But i don't know how to call the this-property. The object-instance can be anything, a string, Dictionary, ...
Does any one has an idea, how to solve the problem?
EDIT:
The GetValue method should do the follwing task, but dynamically over reflection:
public object GetValue(SOP sop, object value)
{
// Non relfection, lets say 'value' is a Dicitionary<string, string>
// return value["key"];
// The type of 'value' is unknown, and the index-parameter (the keys) are accessable over GetIndexArray()
}
EDIT2:
In c# every getter and setter can be invoked via reflection as a method. Is there a way, to get the methods for the "this" property? If it's possible, the problem can be solved with invoking the mesthods.
Call of the GetValue method
object pInst = parentInstance.GetType().GetProperty(VariableName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic).GetValue(parentInstance, null);
var indexChldren = FindChildrenOfType<IndexNode>();
if (indexChldren.Count > 0)
{
pInst = indexChldren[0].GetValue(sop, pInst);
}
I decided to remake the post, since my previous one didn't correctly do what it was supposed to do.
To check which indexers are on an object, you can use the following, which will
Return null when object is null
Return an empty IList when no indexers are found
Return a list of MethodInfos with the found indexers
The trick to check it, is to check if a property has any GetIndexParameters();
public IList<MethodInfo> GetIndexProperties(object obj)
{
if (obj == null)
{
return null;
}
var type = obj.GetType();
IList<MethodInfo> results = new List<MethodInfo>();
try
{
var props = type.GetProperties(System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance);
if (props != null)
{
foreach (var prop in props)
{
var indexParameters = prop.GetIndexParameters();
if (indexParameters == null || indexParameters.Length == 0)
{
continue;
}
var getMethod = prop.GetGetMethod();
if (getMethod == null)
{
continue;
}
results.Add(getMethod);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return results;
}
This works with a List, Dictionary, string, object with indexers, indexers defined with IndexerNameAttribute
As an example, i used this MainMethod, to try several possibilities
object[] exampleHayStack = new object[] {
"This is a test of an indexer",
new TestIndexer(),
null,
string.Empty,
new ClassIndexer(),
new Dictionary<string, string>() { { "key", "value" } },
new List<string>() { "A", "B", "C", "D", "E", "F", "G" } };
ClassIndexer myIndexer = new ClassIndexer();
foreach (var obj in exampleHayStack)
{
var methods = myIndexer.GetIndexProperties(obj);
if (methods == null || methods.Count == 0)
{
Console.WriteLine("{0} doesn't have any indexers", obj);
continue;
}
Console.WriteLine("Testing {0}", obj);
foreach (MethodInfo mi in methods)
{
IList<object> indexParams = new List<object>();
var requiredParams = mi.GetParameters();
foreach (var par in requiredParams)
{
indexParams.Add(myIndexer.ParamForObject(obj, par));
}
try
{
var result = mi.Invoke(obj, indexParams.ToArray());
Console.WriteLine("Result of requesting ({0}) = {1}", string.Join(",", indexParams), result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.ReadLine();
Which then results into:
Testing This is a test of an indexer
Result of requesting (21) = i
Testing TestReflection.Program+TestIndexer
Result of requesting (53) = 5
Result of requesting (Key) = Key item
doesn't have any indexers
Testing
Exception has been thrown by the target of an invocation.
TestReflection.Program+ClassIndexer doesn't have any indexers
Testing System.Collections.Generic.Dictionary`2[System.String,System.String]
Result of requesting (key) = value
Testing System.Collections.Generic.List`1[System.String]
Result of requesting (5) = F
The complete implementation of the ClassIndexer you can find here, it contains an extra method for getting possible key values (but those you have already)
public class ClassIndexer
{
Random pNext = new Random();
public IList<MethodInfo> GetIndexProperties(object obj)
{
if (obj == null)
{
return null;
}
var type = obj.GetType();
IList<MethodInfo> results = new List<MethodInfo>();
try
{
var props = type.GetProperties(System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance);
if (props != null)
{
foreach (var prop in props)
{
var indexParameters = prop.GetIndexParameters();
if (indexParameters == null || indexParameters.Length == 0)
{
continue;
}
var getMethod = prop.GetGetMethod();
if (getMethod == null)
{
continue;
}
results.Add(getMethod);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return results;
}
public object ParamForObject(object obj, ParameterInfo pi)
{
if (obj is IDictionary)
{
int maxNumber = ((IDictionary)obj).Keys.Count;
if (pi.ParameterType.Equals(typeof(int)))
{
return pNext.Next(maxNumber);
}
if (pi.ParameterType.Equals(typeof(string)))
{
int target = pNext.Next(maxNumber);
foreach (var key in ((IDictionary)obj).Keys)
{
target--;
if (target <= 0)
{
return key;
}
}
return null;
}
}
if (obj is string)
{
if (pi.ParameterType.Equals(typeof(int)))
{
return pNext.Next((obj as string).Length);
}
}
if (obj is IList)
{
return pNext.Next(((IList)obj).Count);
}
if (pi.ParameterType.Equals(typeof(string)))
{
return "Key";
}
if (pi.ParameterType.Equals(typeof(int)))
{
return pNext.Next(100);
}
return null;
}
public ClassIndexer()
{
}
}
For the rest, this was a good research, thanks for the question !
I have a class named Configurationwhich inherits from DynamicObject. It it a Dictionary<string, object>. In the constructor, it reads a text file and loads all the values from it splitted by =, so you can create dynamic configuration objects like this:
dynamic configuration = new Configuration("some_file.ini");
Here is my full class:
public sealed class Configuration : DynamicObject
{
private string Path { get; set; }
private Dictionary<string, object> Dictionary { get; set; }
public Configuration(string fileName)
{
this.Path = fileName;
this.Dictionary = new Dictionary<string, object>();
this.Populate();
}
~Configuration()
{
if (this.Dictionary != null)
{
this.Dictionary.Clear();
}
}
private void Populate()
{
using (StreamReader reader = new StreamReader(this.Path))
{
string line;
string currentSection = string.Empty;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line.StartsWith("[") && line.EndsWith("]"))
{
currentSection = line.Trim('[', ']');
}
else if (line.Contains("="))
{
this.Dictionary.Add(string.Format("{0}{1}{2}",
currentSection,
(currentSection != string.Empty) ? "_" : string.Empty,
line.Split('=')[0].Trim()),
ParseValue(line.Split('=')[1].Trim().Split(';')[0]));
}
}
}
}
private object ParseValue(string value)
{
if (string.IsNullOrWhiteSpace(value))
return string.Empty;
if (value.StartsWith("\"") && value.EndsWith("\""))
return value.Substring(1, value.Length - 1);
if (IsNumeric(value))
return Convert.ToInt32(value);
// TODO: FIXME Floating values (not to be confuse with IPAddress).
//if (IsFloating(value))
// return Convert.ToDouble(value);
if (string.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0)
return true;
if (string.Compare(value, "false", StringComparison.OrdinalIgnoreCase) == 0)
return false;
return value;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (this.Dictionary.ContainsKey(binder.Name))
{
if (this.Dictionary[binder.Name] == null)
{
result = null;
}
else
{
result = this.Dictionary[binder.Name];
}
return true;
}
throw new ConfigurationException(binder.Name);
}
public override string ToString()
{
string result = this.Path + " [ ";
int processed = 0;
foreach (KeyValuePair<string, object> value in this.Dictionary)
{
result += value.Key;
processed++;
if (processed < this.Dictionary.Count)
{
result += ", ";
}
}
result += " ]";
return result;
}
private static bool IsNumeric(string value)
{
foreach (char c in value)
if (!char.IsDigit(c))
return false;
return true;
}
private static bool IsFloating(string value)
{
foreach (char c in value)
if (!char.IsDigit(c) && c != '.')
return false;
return true;
}
private class NullObject { }
}
Everything is working flawlessly. I've overriden TryGetMember and I'm returning the object based on the GetMemberBinder Name property. However, I'm facing a problem.
When I execute the following code:
string s = "some_config_key";
string d = configuration.s;
It retrieves the value of the key s rather than some_config_key, meaning that it doesn't evalute the value of the variable s. Is there a workaround for this?
Thanks.
C# dynamic features are partially compiled like the property/method access. To understand this lets take an example.
dynamic myVar = new { a=1, b="test" };
myVar.a += 1;
Here the C# type system would not test for the validity of the property while compilation, only at runtime the code is executed and if the property is not found it will through you runtime error.
So in your code the property access is already complied to access the property named "s" on the dynamic object "configuration".
In C# no-way you can do this not even in dynamically typed languages like python or javascript.
You have to use like this.
dynamic configuration = getConfig("path/to/file");
var myobj = configuration as IDictionary<string,object>;
string s = "config_key";
var value = myobj[s]; // this is correct.
I am trying to look for a value in a complex object. Does anyone know if there is a helper class out there that does this already?
Would like to be able to search an object, while observing endless loop protection to find a tostring value that contains "foo" and then to return the property path that foo was found in. Or an array of paths foo was found in.
So if someone has handy existing code before I undertake this myself; I would be most grateful.
The key is you want to keep a Stack of the current property while you recursively search for the value you're after and also a HashSet of visited objects and skip them. You also want to be careful with your exception handling so an exception in the middle doesn't mess up the stack.
public static string[] FindPathToProperty(object item, string propertyValueToFind)
{
var pathToProperty = new Stack<string>();
var visitedObjects = new HashSet<object> {item};
FindPathToProperty(item, propertyValueToFind, pathToProperty, visitedObjects);
var finalPath = pathToProperty.ToArray();
Array.Reverse(finalPath);
return finalPath;
}
private static bool FindPathToProperty(object item, string propertyValueToFind, Stack<string> pathToProperty, HashSet<object> visitedObjects)
{
foreach (var property in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
try
{
var value = property.GetValue(item, null);
if (visitedObjects.Contains(value))
{
continue;
}
visitedObjects.Add(value);
pathToProperty.Push(property.Name);
bool found = false;
try
{
found = propertyValueToFind.Equals(value) ||
FindPathToProperty(value, propertyValueToFind, pathToProperty, visitedObjects);
}
finally
{
if (!found)
{
pathToProperty.Pop();
}
}
if (found)
{
return true;
}
}
catch
{
continue;
}
}
return false;
}
public static void Test()
{
Test(new { X = "find" }, "X");
Test(new { X = "no", Y = "find" }, "Y");
Test(new { A = new { X = "no", Y = "find" } }, "A.Y");
}
private static void Test(object item, string expected)
{
string actual = string.Join(".", FindPathToProperty(item, "find"));
Console.WriteLine("{0} : {1}\r\n {2}",
actual == expected ? "ok " : "BAD",
expected,
actual);
}