Example #1
0
 public ValueOfSomeTypeExpression(ValueOfSomeTypeExpressionRoot root, Type type, TreeExpression path,
                                  Kind2 kind2)
 {
     Root       = root;
     Type       = type;
     Expression = path;
     Kind2      = kind2;
 }
        private void Scan(ValueOfSomeTypeExpressionRoot root, XUnitTypeName x, Kind2 kind, ExpressionPath path = null)
        {
            if (!_resolver.TryGetValue(x.GetTypename(), out var type))
            {
                throw new NotImplementedException();
            }
            var info1 = new ValueOfSomeTypeExpression(root, type, new TreeExpression(path, null, kind), kind);

            _sink.Add(info1);
            if (kind != Kind2.Property)
            {
                return;
            }
            foreach (var propInfo in type.GetProperties())
            {
                var attribute = propInfo.GetCustomAttribute <RelatedUnitSourceAttribute>();
                if (attribute != null && attribute.Usage == RelatedUnitSourceUsage.DoNotUse)
                {
                    continue;
                }
                var pt = propInfo.PropertyType;
                if (!pt.Implements <IUnit>())
                {
                    continue;
                }
                Scan(root, new XUnitTypeName(propInfo.PropertyType.Name), kind, path + propInfo.Name);
            }

            foreach (var methodInfo in type.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                var at = methodInfo.GetCustomAttribute <RelatedUnitSourceAttribute>();
                if (at is null || (at.Usage & RelatedUnitSourceUsage.ProvidesRelatedUnit) == 0)
                {
                    continue;
                }
                var returnType = methodInfo.ReturnType;
                if (!returnType.Implements <IUnit>())
                {
                    throw new Exception("Should return IUnit");
                }
                if (methodInfo.GetParameters().Length != 0)
                {
                    throw new Exception("Should be parameterles");
                }
                Scan(root, new XUnitTypeName(returnType.Name), Kind2.Method, path + $"{methodInfo.Name}");
            }
        }