/// <summary> /// Convert wmi value data into .net friendly values. /// </summary> /// <param name="wmivalue">raw wmi type.</param> /// <param name="myProp"></param> /// <returns></returns> internal static object ConvertObject(object wmivalue, clsMyProperty myProp) // MyTypeInfoEnum detailinfo, bool nullable = false) { //TODO: mayby change backup function prameters to before with not whole clsMyProperty?? switch (myProp.DetailInfo) { case MyTypeInfoEnum.Guid: return(ConvertGuid(wmivalue)); case MyTypeInfoEnum.DateTime: return(ConvertDateTime(wmivalue, myProp.CustomFormat)); //return ManagementDateTimeConverter.ToDateTime(wmivalue.ToString()); case MyTypeInfoEnum.TimeSpan: return(ManagementDateTimeConverter.ToTimeSpan(wmivalue.ToString())); case MyTypeInfoEnum.Version: return(ConvertVersion(wmivalue)); case MyTypeInfoEnum.Enum: return(ConvertEnum(wmivalue, myProp)); default: return(wmivalue); // no conversion needed. hopefully. } }
internal clsMyType(Type t) { // create mytype. this.TypeName = t.Name; this.FullName = t.FullName; // set wmi class name to use. var attribWmiClassName = t.GetCustomAttribute<WmiClassNameAttribute>(); if (attribWmiClassName != null) this.WmiClassName = attribWmiClassName.WmiClassName; else this.WmiClassName = t.Name; // Compile createobject. //this.CreateObject = Reflection.CompileCreateObject(t); this.CreateObject = Reflection.Instance.TryGetCreateObject(t.FullName, t); var props = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.SetProperty foreach(var p in props) { var myprop = new clsMyProperty(p); WmiProperties.Add(myprop.WmiName, myprop); } }
internal clsMyType(Type t) { // create mytype. this.TypeName = t.Name; this.FullName = t.FullName; // set wmi class name to use. var attribWmiClassName = t.GetCustomAttribute <WmiClassNameAttribute>(); if (attribWmiClassName != null) { this.WmiClassName = attribWmiClassName.WmiClassName; } else { this.WmiClassName = t.Name; } // Compile createobject. //this.CreateObject = Reflection.CompileCreateObject(t); this.CreateObject = Reflection.Instance.TryGetCreateObject(t.FullName, t); var props = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); // | BindingFlags.SetProperty foreach (var p in props) { var myprop = new clsMyProperty(p); WmiProperties.Add(myprop.WmiName, myprop); } }
/// <summary> /// Creates a generic list and adds array into it. /// </summary> /// <param name="wmivalue"></param> /// <param name="prop"></param> /// <returns></returns> internal static object CreateGenericList(object wmivalue, clsMyProperty prop) { // retrive or compile new generic list create delegate. var create = Reflection.Instance.TryGetCreateObject(prop.RefType.FullName, prop.RefType); // create array. var list = (System.Collections.IList)create(); // cast to object[] //TODO: cast to array instead? var array = (object[])wmivalue; foreach (var obj in array) { // convert objects in case of guid list or datetime list //var o = ConvertObject(obj, prop.DetailInfo, prop.IsNullable); var o = ConvertObject(obj, prop); // add list.Add(o); } return(list); }
/// <summary> /// Creates a /// </summary> /// <param name="wmivalue"></param> /// <param name="prop"></param> /// <returns></returns> internal static object CreateArray(object wmivalue, clsMyProperty prop) { //TODO: Find out if we can in some cases just set whoe array directly? var array = (Array)wmivalue; // cast to array. // create new array of base type and same length. var result = Array.CreateInstance(prop.BaseType, array.Length); int current = 0; // enumerate wmi array, foreach (var obj in array) { // convert objects in case of guid array or datetime arrays //var o = ConvertObject(obj, prop.DetailInfo, prop.IsNullable); var o = ConvertObject(obj, prop); // set object. result.SetValue(o, current++); } return(result); }
private static object ConvertEnum(object wmiValue, clsMyProperty myProp) { //TODO: handle conversion errors. return Enum.Parse(myProp.RefType, wmiValue.ToString()); //return null; }
/// <summary> /// Creates a generic list and adds array into it. /// </summary> /// <param name="wmivalue"></param> /// <param name="prop"></param> /// <returns></returns> internal static object CreateGenericList(object wmivalue, clsMyProperty prop) { // retrive or compile new generic list create delegate. var create = Reflection.Instance.TryGetCreateObject(prop.RefType.FullName, prop.RefType); // create array. var list = (System.Collections.IList)create(); // cast to object[] //TODO: cast to array instead? var array = (object[])wmivalue; foreach(var obj in array) { // convert objects in case of guid list or datetime list //var o = ConvertObject(obj, prop.DetailInfo, prop.IsNullable); var o = ConvertObject(obj, prop); // add list.Add(o); } return list; }
/// <summary> /// Creates a /// </summary> /// <param name="wmivalue"></param> /// <param name="prop"></param> /// <returns></returns> internal static object CreateArray(object wmivalue, clsMyProperty prop) { //TODO: Find out if we can in some cases just set whoe array directly? var array = (Array)wmivalue; // cast to array. // create new array of base type and same length. var result = Array.CreateInstance(prop.BaseType, array.Length); int current = 0; // enumerate wmi array, foreach(var obj in array) { // convert objects in case of guid array or datetime arrays //var o = ConvertObject(obj, prop.DetailInfo, prop.IsNullable); var o = ConvertObject(obj, prop); // set object. result.SetValue(o, current++); } return result; }
// MyTypeInfoEnum detailinfo, bool nullable = false) /// <summary> /// Convert wmi value data into .net friendly values. /// </summary> /// <param name="wmivalue">raw wmi type.</param> /// <param name="myProp"></param> /// <returns></returns> internal static object ConvertObject(object wmivalue, clsMyProperty myProp) { //TODO: mayby change backup function prameters to before with not whole clsMyProperty?? switch (myProp.DetailInfo) { case MyTypeInfoEnum.Guid: return ConvertGuid(wmivalue); case MyTypeInfoEnum.DateTime: return ConvertDateTime(wmivalue, myProp.CustomFormat); //return ManagementDateTimeConverter.ToDateTime(wmivalue.ToString()); case MyTypeInfoEnum.TimeSpan: return ManagementDateTimeConverter.ToTimeSpan(wmivalue.ToString()); case MyTypeInfoEnum.Version: return ConvertVersion(wmivalue); case MyTypeInfoEnum.Enum: return ConvertEnum(wmivalue, myProp); default: return wmivalue; // no conversion needed. hopefully. } }
/// <summary> /// Convert a list of ManagementBaseObjects into our types. /// </summary> /// <param name="data"></param> /// <param name="myType"></param> /// <param name="result"></param> /// <returns></returns> internal static bool Convert(List <ManagementBaseObject> data, clsMyType myType, System.Collections.IList result) { bool flagPerfectConversion = true; if (data == null) { throw new ArgumentNullException(string.Format("Argument cant be null. Arg='{0}', Type='{1}'", nameof(data), typeof(ManagementObjectCollection).Name)); } if (data.Count == 0) { return(true); } foreach (var item in data) { // Create the instance. var instance = myType.CreateObject(); foreach (var p in item.Properties) { //if (!p.IsLocal) // are there any info in this propname?? // continue; // no, then continue. clsMyProperty myprop = null; // try to match wmi prop against our props. if (myType.WmiProperties.TryGetValue(p.Name, out myprop)) { if (myprop.DetailInfo == MyTypeInfoEnum.Invalid) // ignore types we dont know how to handle. { flagPerfectConversion = false; // set not perfect conversion flag. continue; } object oset = null; if (myprop.IsArray) { if (myprop.CimType == p.Type) { oset = p.Value; } else { oset = CreateArray(p.Value, myprop); } } else if (myprop.IsList) { oset = CreateGenericList(p.Value, myprop); } else { oset = ConvertObject(p.Value, myprop); } //oset = ConvertObject(p.Value, myprop.DetailInfo, myprop.IsNullable); //if (myprop.DetailInfo != MyTypeInfoEnum.Invalid) if (myprop.IsNullable || oset != null) { instance = myprop.GenericSetter(instance, oset); } } } result.Add(instance); } return(flagPerfectConversion); }
private static object ConvertEnum(object wmiValue, clsMyProperty myProp) { //TODO: handle conversion errors. return(Enum.Parse(myProp.RefType, wmiValue.ToString())); //return null; }