protected override Expression VisitMethodCall(MethodCallExpression m) { switch (m.Method.Name) { case "Where": this.Visit(m.Arguments[0]); sb.Append(" WHERE "); LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]); this.Visit(lambda.Body); return(m); case "Associators": var arg = (ConstantExpression)m.Arguments[0]; sb.Append("ASSOCIATORS OF {"); sb.Append(WmiObject.GetClassName(arg.Value.GetType())); sb.Append("=\""); sb.Append(WmiObject.GetWmiQueryValue(((WmiObject)arg.Value).ID)); sb.Append("\"} WHERE ResultClass="); sb.Append(WmiObject.GetClassName(m.Method.GetGenericArguments()[0])); return(m); case "Contains": this.Visit(m.Object); sb.Append(" LIKE \"%"); var oldSB = sb; sb = new StringBuilder(); this.Visit(m.Arguments[0]); var s = sb.ToString(); sb = oldSB; sb.Append(s.Substring(1, s.Length - 2)); sb.Append("%\""); return(m); } throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name)); }
public static IEnumerable <TResult> Associators <TResult>(this WmiObject source) where TResult : WmiObject <TResult>, new() { var q = new WmiQuery <TResult>(source.CreationContext, Expression.Call(((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( typeof(TResult)), Expression.Constant(source, typeof(WmiObject)))); return(q); }
protected override Expression VisitConstant(ConstantExpression c) { IWmiQueryable q = c.Value as IWmiQueryable; if (q != null) { // assume constant nodes w/ IQueryables are table references if (sb.Length == 0) { sb.Append("SELECT "); if (m_FetchAllFields) { sb.Append("*"); } else { sb.Append(string.Join(",", WmiObject.GetWmiPropertyNames(q.ElementType))); } sb.Append(" FROM "); } sb.Append(WmiObject.GetClassName(q.ElementType)); } else if (c.Value == null) { sb.Append("NULL"); } else { switch (Type.GetTypeCode(c.Value.GetType())) { case TypeCode.Boolean: sb.Append(((bool)c.Value) ? 1 : 0); break; case TypeCode.String: sb.AppendFormat("\"{0}\"", WmiObject.GetWmiQueryValue(c.Value)); break; case TypeCode.Object: throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", c.Value)); default: sb.Append(c.Value); break; } } return(c); }
public IEnumerator <TResult> Execute <TResult>(IWmiQueryable query) where TResult : WmiObject <TResult>, new() { if (m_QueryBuilder == null) { m_QueryBuilder = new WmiQueryBuilder(this); } var querystring = m_QueryBuilder.Translate(query, m_FetchAllFields); var objQuery = new ObjectQuery(querystring); var wmiSearcher = new ManagementObjectSearcher(m_Scope, objQuery); Debug.WriteLine(querystring); foreach (ManagementObject o in wmiSearcher.Get().Cast <ManagementObject>()) { yield return(WmiObject.CreateObject <TResult>(this, o)); } wmiSearcher.Dispose(); }
protected override Expression VisitMember(MemberExpression m) { if (m.Expression != null && m.Expression.NodeType == ExpressionType.Parameter) { if (m.Member.Name == "ID") { sb.Append(WmiObject.GetKeyPropertyName(m.Expression.Type)); } else { var a = m.Member.GetCustomAttribute <WmiPropertyAttribute>(); if (a == null) { throw new MemberAccessException(string.Format("'{0}' is not a WMI property of class '{1}'", m.Member.Name, m.Type.Name)); } sb.Append(a.Property ?? m.Member.Name); } return(m); } throw new NotSupportedException(string.Format("The member '{0}' is not supported", m.Member.Name)); }