public IEnumerator <IDuckKeyValue <K, T> > GetEnumerator() { object[] ducktypek = typeof(K).GetCustomAttributes(typeof(DuckTypeAttribute), false); object[] ducktypev = typeof(T).GetCustomAttributes(typeof(DuckTypeAttribute), false); bool isKeyDuck = (ducktypek.Length > 0); bool isValDuck = (ducktypev.Length > 0); while (_en.MoveNext()) { K k; T t; if (isKeyDuck) { k = DuckTyping.Instance.ImplementDuckType <K>(_en.Key); } else { ConvertToClr.ConversionStrictness str; k = ConvertToClr.TryObjectToType <K>(_en.Key, out str); if (str == ConvertToClr.ConversionStrictness.Failed) { throw new InvalidCastException(); } } if (isValDuck) { t = DuckTyping.Instance.ImplementDuckType <T>(_en.Value); } else { ConvertToClr.ConversionStrictness str; t = ConvertToClr.TryObjectToType <T>(_en.Value, out str); if (str == ConvertToClr.ConversionStrictness.Failed) { throw new InvalidCastException(); } } yield return(new DuckKeyValue <K, T>(k, t)); } }
public IEnumerator <T> GetEnumerator() { object[] ducktype = typeof(T).GetCustomAttributes(typeof(DuckTypeAttribute), false); bool isDuck = (ducktype.Length > 0); while (_en.MoveNext()) { if (isDuck) { yield return(DuckTyping.Instance.ImplementDuckType <T>(_en.Value)); } else { ConvertToClr.ConversionStrictness str; T ret = ConvertToClr.TryObjectToType <T>(_en.Value, out str); if (str == ConvertToClr.ConversionStrictness.Failed) { throw new InvalidCastException(); } yield return(ret); } } }
public void Emit(CodeGenerator /*!*/ codeGen, IPhpCustomAttributeProvider /*!*/ target, bool force) { // skip attributes that are not emitted: if (!isEmitted && !force) { return; } ConstructorInfo real_ctor; // TODO: type conversions (in analysis during overload resolution?) var parameters = node.CallSignature.Parameters; Type[] real_ctor_parameter_types = new Type[parameters.Length]; if (type is ClrType) { real_ctor = (ConstructorInfo)((ClrMethod.Overload)overload).Method; var real_params = real_ctor.GetParameters(); for (int i = 0; i < real_ctor_parameter_types.Length; i++) { real_ctor_parameter_types[i] = real_params[i].ParameterType; } } else { Debug.Assert(type is PhpType); real_ctor = ((PhpType)type).ClrConstructorInfos[0]; // Do not try to call GetParameters(), all parameters are of type object, // GetParameters() of not baked PhpType throws. for (int i = 0; i < real_ctor_parameter_types.Length; i++) { real_ctor_parameter_types[i] = PHP.Core.Emit.Types.Object[0]; } } // ctor args: object[] ctor_args = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { Expression expr = parameters[i].Expression; TypeOfEx type_of = expr as TypeOfEx; if (type_of != null) { ctor_args[i] = type_of.ClassNameRef.ResolvedType().RealType; } else { ctor_args[i] = ConvertToClr.ObjectToType(expr.GetValue(), real_ctor_parameter_types[i]); } } List <FieldInfo> fields = new List <FieldInfo>(); List <PropertyInfo> properties = new List <PropertyInfo>(); List <object> field_values = new List <object>(); List <object> prop_values = new List <object>(); foreach (NamedActualParam param in node.NamedParameters) { object value = param.Expression.GetValue(); MemberInfo real_member = param.GetProperty().RealMember; FieldInfo real_field; PropertyInfo real_property; if ((real_property = real_member as PropertyInfo) != null || // regular CLR property (param.GetProperty() is PhpField && (real_property = ((PhpField)param.GetProperty()).ExportedProperty) != null)) // or PHP property (real field below is PhpReference, we have to use its export stub) { properties.Add(real_property); prop_values.Add(ConvertToClr.ObjectToType(value, real_property.PropertyType)); } else if ((real_field = real_member as FieldInfo) != null) { fields.Add(real_field); field_values.Add(ConvertToClr.ObjectToType(value, real_field.FieldType)); } else { Debug.Fail("Cannot resolve attribute named parameter!"); } } CustomAttributeBuilder builder = new CustomAttributeBuilder(real_ctor, ctor_args, properties.ToArray(), prop_values.ToArray(), fields.ToArray(), field_values.ToArray()); switch (node.TargetSelector) { case CustomAttribute.TargetSelectors.Assembly: case CustomAttribute.TargetSelectors.Module: codeGen.CompilationUnit.ModuleBuilder.AssemblyBuilder.EmitCustomAttribute(builder, node.TargetSelector); break; default: target.EmitCustomAttribute(builder, node.TargetSelector); break; } }