Skip to content

Commit

Permalink
Improved async operations. Add events support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas committed Jun 6, 2018
1 parent e249898 commit b19b297
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 140 deletions.
2 changes: 1 addition & 1 deletion ORMi.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static void Main(string[] args)
{
WMIHelper helper = new WMIHelper("root\\CimV2");

List<Processor> processors = helper.Query<Processor>().ToList();
var processors = helper.QueryAsync<Processor>();

Person person = new Person
{
Expand Down
161 changes: 161 additions & 0 deletions ORMi/Helpers/TypeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ORMi.Helpers
{
public static class TypeHelper
{
public static object LoadObject(ManagementObject mo, Type t)
{
var o = Activator.CreateInstance(t);

foreach (PropertyInfo p in o.GetType().GetProperties())
{
WMIProperty propAtt = p.GetCustomAttribute<WMIProperty>();

string propertyName = String.Empty;

if (propAtt != null)
{
propertyName = propAtt.Name;
}
else
{
propertyName = p.Name;
}

var a = mo.Properties[propertyName].Value;

p.SetValue(o, Convert.ChangeType(a, p.PropertyType), null);
}

return o;
}

public static object LoadObject(ManagementBaseObject mo, Type t)
{
var o = Activator.CreateInstance(t);

foreach (PropertyInfo p in o.GetType().GetProperties())
{
WMIProperty propAtt = p.GetCustomAttribute<WMIProperty>();

string propertyName = String.Empty;

if (propAtt != null)
{
propertyName = propAtt.Name;
}
else
{
propertyName = p.Name;
}

var a = mo.Properties[propertyName].Value;

p.SetValue(o, Convert.ChangeType(a, p.PropertyType), null);
}

return o;
}

public static string GetClassName(object p)
{
var dnAttribute = p.GetType().GetCustomAttributes(
typeof(WMIClass), true
).FirstOrDefault() as WMIClass;
if (dnAttribute != null)
{
return dnAttribute.Name;
}
else
{
return p.GetType().Name;
}
}

public static string GetClassName(Type t)
{
var dnAttribute = t.GetCustomAttributes(
typeof(WMIClass), true
).FirstOrDefault() as WMIClass;
if (dnAttribute != null)
{
return dnAttribute.Name;
}
return null;
}

public static ManagementObject GetManagementObject(ManagementClass sourceClass, object obj)
{
ManagementObject genericInstance = sourceClass.CreateInstance();

foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
{
WMIProperty propAtt = propertyInfo.GetCustomAttribute<WMIProperty>();

if (propAtt == null)
{
if (propertyInfo.GetValue(obj).GetType() == typeof(DateTime))
{
genericInstance[propertyInfo.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
}
else
{
genericInstance[propertyInfo.Name.ToUpper()] = propertyInfo.GetValue(obj);
}
}
else
{
if (propertyInfo.GetValue(obj).GetType() == typeof(DateTime))
{
genericInstance[propAtt.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
}
else
{
genericInstance[propAtt.Name.ToUpper()] = propertyInfo.GetValue(obj);
}
}
}

return genericInstance;
}

public static WMISearchKey GetSearchKey(object p)
{
WMISearchKey res = null;

foreach (PropertyInfo propertyInfo in p.GetType().GetProperties())
{
WMIProperty propAtt = propertyInfo.GetCustomAttribute<WMIProperty>();

if (propAtt != null)
{
if (propAtt.SearchKey)
{
res = new WMISearchKey
{
Name = propAtt.Name,
Value = propertyInfo.GetValue(p)
};

break;
}
}
}

return res;
}
}

public class WMISearchKey
{
public string Name { get; set; }
public object Value { get; set; }
}
}
2 changes: 2 additions & 0 deletions ORMi/ORMi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\TypeHelper.cs" />
<Compile Include="WMIHelper.cs" />
<Compile Include="WMIProperty.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WMIWatcher.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit b19b297

Please sign in to comment.