public bool Apply(DynamicType glymaNode, IGlymaMapRepository mapRepository) { bool includeNode = true; if (glymaNode == null) { throw new ArgumentNullException("glymaNode"); } string nodeType = (string)glymaNode[GlymaEntityFields.NodeType]; if (nodeType.Equals(GlymaNodeTypes.Question, StringComparison.OrdinalIgnoreCase)) { string nodeName = (string)glymaNode[GlymaEntityFields.Name]; if (!string.IsNullOrEmpty(nodeName)) { foreach (string specifiedQuestion in SpecifiedQuestions) { if (nodeName.Equals(specifiedQuestion, StringComparison.OrdinalIgnoreCase)) { includeNode = false; break; } } } else { includeNode = false; } } return includeNode; }
public bool Apply(DynamicType glymaNode, IGlymaMapRepository mapRepository) { bool includeNode = true; if (glymaNode == null) { throw new ArgumentNullException("glymaNode"); } string nodeType = (string)glymaNode[GlymaEntityFields.NodeType]; if (nodeType.Equals(GlymaNodeTypes.Question, StringComparison.OrdinalIgnoreCase)) { string nodeName = (string)glymaNode[GlymaEntityFields.Name]; if (!string.IsNullOrEmpty(nodeName)) { // Apply default logic that excludes nodes that are less than two "words". The number of words is determined by the number of spaces. int wordCount = nodeName.Count(f => f == ' ') + 1; if (wordCount <= 2) { includeNode = false; } } else { includeNode = false; } } return includeNode; }
public bool Apply(DynamicType glymaNode, IGlymaMapRepository mapRepository) { bool includeNode = true; List<GlymaNodeReference> parentNodes = new List<GlymaNodeReference>(); string parentNodesJson = string.Empty; if (glymaNode == null) { throw new ArgumentNullException("glymaNode"); } Guid mapId = (Guid)glymaNode[GlymaEntityFields.MapId]; Guid nodeId = (Guid)glymaNode[GlymaEntityFields.Id]; if (nodeId.Equals(Guid.Empty)) { throw new ArgumentException("The node ID is invalid because it has an undefined value."); } parentNodes = mapRepository.GetParentNodes(mapId, nodeId); if (parentNodes.Count > 0) { parentNodes.Sort(new NodeComparer()); JavaScriptSerializer serialiser = new JavaScriptSerializer(); parentNodesJson = serialiser.Serialize(parentNodes); } glymaNode[GlymaEntityFields.ParentNodes] = parentNodesJson; return includeNode; }
public bool SerializeAndDeserializeOnTwoAppDomains(DynamicType domainOneType, DynamicType domainTwoType, VersionToleranceLevel vtl, bool allowGuidChange = true) { testsOnDomain1.CreateInstanceOnAppDomain(domainOneType); testsOnDomain2.CreateInstanceOnAppDomain(domainTwoType); var bytes = testsOnDomain1.SerializeOnAppDomain(); try { testsOnDomain2.DeserializeOnAppDomain(bytes, settings.GetSettings(allowGuidChange ? vtl | VersionToleranceLevel.AllowGuidChange : vtl)); return true; } catch (VersionToleranceException) { return false; } }
public void ShouldBeAbleToShareTheSameDynamicType() { var typeSpec = new TypeSpec { Name = "Person" }; // Add an age property typeSpec.AddProperty("Age", typeof(int)); // Attach the DynamicType named 'Person' to a bunch of dynamic objects var personType = new DynamicType(typeSpec); var first = new DynamicObject(); var second = new DynamicObject(); first += personType; second += personType; // Use both objects as persons var firstPerson = first.CreateDuck<IPerson>(); var secondPerson = second.CreateDuck<IPerson>(); firstPerson.Age = 18; secondPerson.Age = 21; Assert.AreEqual(18, firstPerson.Age); Assert.AreEqual(21, secondPerson.Age); // Change the type so that it supports the INameable interface typeSpec.AddProperty("Name", typeof(string)); var firstNameable = first.CreateDuck<INameable>(); var secondNameable = second.CreateDuck<INameable>(); firstNameable.Name = "Foo"; secondNameable.Name = "Bar"; Assert.AreEqual("Foo", firstNameable.Name); Assert.AreEqual("Bar", secondNameable.Name); }
public DynamicType InvokeMethod(string IIDS_method, DynamicType a0 = null, DynamicType a1 = null, DynamicType a2 = null, DynamicType a3 = null, DynamicType a4 = null, DynamicType a5 = null, DynamicType a6 = null, DynamicType a7 = null, DynamicType a8 = null, DynamicType a9 = null) { return(new DynamicType(Module_invokeMethod(GetNativeReference(), IIDS_method, a0?.GetNativeReference() ?? IntPtr.Zero, a1?.GetNativeReference() ?? IntPtr.Zero, a2?.GetNativeReference() ?? IntPtr.Zero, a3?.GetNativeReference() ?? IntPtr.Zero, a4?.GetNativeReference() ?? IntPtr.Zero, a5?.GetNativeReference() ?? IntPtr.Zero, a6?.GetNativeReference() ?? IntPtr.Zero, a7?.GetNativeReference() ?? IntPtr.Zero, a8?.GetNativeReference() ?? IntPtr.Zero, a9?.GetNativeReference() ?? IntPtr.Zero))); }
private void BuildNestedType(DynamicType parentType, TypeDefinition typeDefinition) { DynamicType type = parentType.DefineNestedType(typeDefinition.Name, (System.Reflection.TypeAttributes)typeDefinition.Attributes, null, (int)typeDefinition.ClassSize); // TODO: How do we set PackingSize? InitializeType(type, typeDefinition); }
public void CreateInstanceOnAppDomain(DynamicType type, Version version = null) { obj = type.Instantiate(version); }
public DynamicType DefineType(string name, TypeAttributes attr, Type parent, PackingSize packingSize, int typesize) { TypeBuilder typeBuilder = builder.DefineType(name, attr, parent, packingSize, typesize); DynamicType type = new DynamicType(typeBuilder, this); return type; }
private void BuildConstructor(DynamicType type, MethodDefinition methodDefinition) { DynamicConstructor constructor = type.DefineConstructor( (System.Reflection.MethodAttributes) methodDefinition.Attributes, CecilUtils.GetCallingConventions(methodDefinition), ResolveParameterTypes(methodDefinition.Parameters)); // TODO: Custom modifiers and other stuff. InitializeParameters(constructor.Builder.DefineParameter, methodDefinition.Parameters); constructor.Builder.SetImplementationFlags((System.Reflection.MethodImplAttributes) methodDefinition.ImplAttributes); metadataPass.Add(delegate { InitializeDeclarativeSecurity(constructor.Builder.AddDeclarativeSecurity, methodDefinition.SecurityDeclarations); InitializeCustomAttributes(constructor.Builder.SetCustomAttribute, methodDefinition.CustomAttributes); }); if (methodDefinition.HasBody) { ilPass.Add(delegate { constructor.Builder.InitLocals = methodDefinition.Body.InitLocals; BuildIL(constructor.Builder, constructor.Builder.GetILGenerator, methodDefinition.Body); }); } }
public EnumValueBlock(string name, DynamicType type) : base(name, type) { }
private static Type CreateDynamicType(DynamicType dynamicType) { Mock<Type> type = new Mock<Type>(); type.Setup(t => t.Name).Returns(dynamicType.TypeName); type.Setup(t => t.Namespace).Returns("SampleNamespace"); type.Setup(t => t.FullName).Returns("SampleNamespace." + dynamicType.TypeName); type .Setup(t => t.GetProperties(It.IsAny<BindingFlags>())) .Returns(dynamicType.Properties.Select(property => CreateProperty(property, type.Object)).Cast<PropertyInfo>().ToArray()); type.Setup(t => t.IsAssignableFrom(type.Object)).Returns(true); type.Setup(t => t.GetHashCode()).Returns(type.GetHashCode()); type.Setup(t => t.Equals(It.IsAny<object>())).Returns((Type t) => Object.ReferenceEquals(type.Object, t)); return type.Object; }
private void BuildProperty(DynamicType type, PropertyDefinition propertyDefinition) { PropertyBuilder propertyBuilder = type.DefineProperty(propertyDefinition.Name, (System.Reflection.PropertyAttributes)propertyDefinition.Attributes, ResolveType(propertyDefinition.PropertyType), ResolveParameterTypes(propertyDefinition.Parameters)); // TODO: Custom modifiers and other stuff. if (propertyDefinition.HasConstant) propertyBuilder.SetConstant(propertyDefinition.Constant); if (propertyDefinition.GetMethod != null) propertyBuilder.SetGetMethod(BuildMethod(type, propertyDefinition.GetMethod)); if (propertyDefinition.SetMethod != null) propertyBuilder.SetSetMethod(BuildMethod(type, propertyDefinition.SetMethod)); metadataPass.Add(delegate { InitializeCustomAttributes(propertyBuilder.SetCustomAttribute, propertyDefinition.CustomAttributes); }); }
public DynamicAction(DynamicType type, MethodInfo methodInfo) { _Type = type; Method = methodInfo; }
public void ModelBuilder_PrunesUnReachableTypes(DynamicType type) { var modelBuilder = new ODataConventionModelBuilder(); Type entityType = CreateDynamicType(type); modelBuilder.AddEntity(entityType); var model = modelBuilder.GetEdmModel(); Assert.True(model.FindType("SampleNamespace.IgnoredType") == null); }
public void SetAttribute(string userData, string name, DynamicType data) { Object_setAttribute(GetNativeReference(), userData, name, data.GetNativeReference()); }
EQXSerializer(Type type) { m_type = type; m_dynamicType = DynamicType.GetDynamicType(m_type); // build the functions here to pay the cost up front, once per type, instead of at runtime // whenever an element is processed ReaderDelegate readAttributes = (XPathNavigator reader, ref object target) => { }; ReaderDelegate readElements = (XPathNavigator reader, ref object target) => { }; ReaderDelegate readArrays = (XPathNavigator reader, ref object target) => { }; WriterDelegate writeAttributes = (XElement result, ref object target) => { }; WriterDelegate writeElements = (XElement result, ref object target) => { }; WriterDelegate writeArrays = (XElement result, ref object target) => { }; for (int i = 0; i < m_dynamicType.Members.Count; i++) { // this goofy setup is to prevent the dynamic methods from acessing the wrong member int temp = i; DynamicMember member = m_dynamicType.Members[temp]; object[] attributes = member.MemberInfo.GetCustomAttributes(false); EqXAttributeAttribute eqxAttribute = null; EqXElementAttribute eqxElement = null; EqXArrayAttribute eqxArray = null; DefaultValueAttribute defaultValue = null; foreach (object attr in attributes) { eqxAttribute = eqxAttribute ?? attr as EqXAttributeAttribute; eqxElement = eqxElement ?? attr as EqXElementAttribute; eqxArray = eqxArray ?? attr as EqXArrayAttribute; defaultValue = defaultValue ?? attr as DefaultValueAttribute; } // count how many of the eqx attributes are present int eqxCount = ((eqxAttribute != null) ? 1 : 0) + ((eqxElement != null) ? 1 : 0) + ((eqxArray != null) ? 1 : 0); #if DEBUG // check how many attributes are present in debug builds to sort out ambiguity if (eqxCount > 1) Trace.TraceWarning(TooManyAttributes, m_type.Name, member.MemberName); #endif // don't process members that aren't marked if (eqxCount < 1) continue; #if DEBUG if (!member.CanWrite && !member.CanRead) { Trace.TraceWarning("'{0}.{1}' has no way to be read or written, skipping it.", m_type.Name, member.MemberName); continue; } else if (!member.CanRead) { Trace.TraceWarning("'{0}.{1}' marked for serialization but can not be read, skipping it.", m_type.Name, member.MemberName); continue; } else if (!member.CanWrite) { Trace.TraceWarning("'{0}.{1}' marked for serialization but can not be written, skipping it.", m_type.Name, member.MemberName); continue; } #endif // find the default value for the member object dvalue = null; if (defaultValue != null) dvalue = defaultValue.Value; if (member.MemberType.IsValueType && dvalue == null) dvalue = Activator.CreateInstance(member.MemberType); if (eqxAttribute != null) { if (!member.MemberType.IsPrimitive && member.MemberType != typeof(string)) throw new InvalidOperationException(string.Format(OnlyPrimitivesXmlAttributes, m_type.Name, member.MemberName)); string name = eqxAttribute.Name ?? member.MemberName; readAttributes += ReadPrimitive(member, string.Concat("@", name), dvalue); writeAttributes += WritePrimitiveAttribute(member, name, dvalue, eqxAttribute.AlwaysWrite); } else if (eqxElement != null) { string name = eqxElement.Name ?? member.MemberName; if (member.MemberType.IsComplex() && member.MemberType != typeof(string)) { readElements += ReadComplex(member, name, dvalue); writeElements += WriteComplexElement(member, name, dvalue, eqxElement.AlwaysWrite); } else { readElements += ReadPrimitive(member, name, dvalue); writeElements += WritePrimitiveElement(member, name, dvalue, eqxElement.AlwaysWrite); } } else if (eqxArray != null) { string name = eqxArray.Name ?? member.MemberName; readElements += ReadArray(member, name, eqxArray.ItemName, dvalue); writeElements += WriteArray(member, name, eqxArray.ItemName, dvalue, eqxArray.AlwaysWrite); } } m_reader = (XPathNavigator navigator, ref object target) => { if (target == null) target = m_dynamicType.CreateInstance(); if (navigator.HasAttributes) readAttributes(navigator, ref target); if (navigator.HasChildren) { readElements(navigator, ref target); readArrays(navigator, ref target); } }; m_writer = (XElement result, ref object target) => { if (target != null) { writeAttributes(result, ref target); writeElements(result, ref target); writeArrays(result, ref target); } }; }
public void RegisterDynamicType(TypeBuilder typeBuilder, DynamicType dynamicType) { dynamicTypes.Add(typeBuilder, dynamicType); }
private void InitializeType(DynamicType type, TypeDefinition typeDefinition) { foreach (TypeDefinition nestedTypeDefinition in typeDefinition.NestedTypes) BuildNestedType(type, nestedTypeDefinition); resolvePass.Add(delegate { if (typeDefinition.BaseType != null) type.Builder.SetParent(ResolveType(typeDefinition.BaseType)); InitializeGenericParameters(type.Builder.DefineGenericParameters, typeDefinition.GenericParameters); foreach (FieldDefinition fieldDefinition in typeDefinition.Fields) BuildField(type, fieldDefinition); foreach (PropertyDefinition propertyDefinition in typeDefinition.Properties) BuildProperty(type, propertyDefinition); foreach (EventDefinition eventDefinition in typeDefinition.Events) BuildEvent(type, eventDefinition); foreach (MethodDefinition methodDefinition in typeDefinition.Methods) BuildMethod(type, methodDefinition); foreach (MethodDefinition methodDefinition in typeDefinition.Constructors) BuildConstructor(type, methodDefinition); }); metadataPass.Add(delegate { InitializeDeclarativeSecurity(type.Builder.AddDeclarativeSecurity, typeDefinition.SecurityDeclarations); InitializeCustomAttributes(type.Builder.SetCustomAttribute, typeDefinition.CustomAttributes); }); }
public abstract DynamicType CreateSequenceType(DynamicType elementType, int bound);
private void BuildField(DynamicType type, FieldDefinition fieldDefinition) { Type fieldType = ResolveType(fieldDefinition.FieldType); FieldBuilder fieldBuilder = type.DefineField(fieldDefinition.Name, fieldType, (System.Reflection.FieldAttributes)fieldDefinition.Attributes); // TODO: Custom modifiers and other stuff. if (fieldDefinition.HasLayoutInfo) fieldBuilder.SetOffset((int) fieldDefinition.Offset); if (fieldDefinition.HasConstant) fieldBuilder.SetConstant(fieldDefinition.Constant); metadataPass.Add(delegate { InitializeCustomAttributes(fieldBuilder.SetCustomAttribute, fieldDefinition.CustomAttributes); }); }
public abstract DynamicType CreateArrayType(DynamicType elementType, params int[] bound);
private void BuildEvent(DynamicType type, EventDefinition eventDefinition) { EventBuilder eventBuilder = type.DefineEvent(eventDefinition.Name, (System.Reflection.EventAttributes) eventDefinition.Attributes, ResolveType(eventDefinition.EventType)); // TODO: Custom modifiers and other stuff. if (eventDefinition.InvokeMethod != null) eventBuilder.SetRaiseMethod(BuildMethod(type, eventDefinition.InvokeMethod)); if (eventDefinition.AddMethod != null) eventBuilder.SetAddOnMethod(BuildMethod(type, eventDefinition.AddMethod)); if (eventDefinition.RemoveMethod != null) eventBuilder.SetRemoveOnMethod(BuildMethod(type, eventDefinition.RemoveMethod)); metadataPass.Add(delegate { InitializeCustomAttributes(eventBuilder.SetCustomAttribute, eventDefinition.CustomAttributes); }); }
public abstract DynamicType CreateMapType(DynamicType keyElementType, DynamicType elementType, int bound);
private MethodBuilder BuildMethod(DynamicType type, MethodDefinition methodDefinition) { MethodBuilder methodBuilder = type.DefineMethod(methodDefinition.Name, (System.Reflection.MethodAttributes) methodDefinition.Attributes, CecilUtils.GetCallingConventions(methodDefinition)); // TODO: Custom modifiers and other stuff. InitializeGenericParameters(methodBuilder.DefineGenericParameters, methodDefinition.GenericParameters); methodBuilder.SetParameters(ResolveParameterTypes(methodDefinition.Parameters)); InitializeParameters(methodBuilder.DefineParameter, methodDefinition.Parameters); InitializeReturnType(methodBuilder, methodDefinition.ReturnType); methodBuilder.SetImplementationFlags((System.Reflection.MethodImplAttributes) methodDefinition.ImplAttributes); metadataPass.Add(delegate { InitializeDeclarativeSecurity(methodBuilder.AddDeclarativeSecurity, methodDefinition.SecurityDeclarations); InitializeCustomAttributes(methodBuilder.SetCustomAttribute, methodDefinition.CustomAttributes); }); if (methodDefinition.HasBody) { ilPass.Add(delegate { methodBuilder.InitLocals = methodDefinition.Body.InitLocals; BuildIL(methodBuilder, methodBuilder.GetILGenerator, methodDefinition.Body); }); } return methodBuilder; }
public void SetAttributeValue(string name, DynamicType value) { DistTransaction_setAttributeValue(GetNativeReference(), name, value.GetNativeReference()); }
public EntityAction(DynamicType type, MethodInfo method) : base(type, method) { }
public Task(Types type, Vec3 position, DynamicType entityType) { this.type = type; this.position = position; this.entityType = entityType; this.entity = null; this.wanderRadius = float.NaN; this.wanderDistance = float.NaN; this.wanderJitter = float.NaN; }
public PropertyBlock(string name, DynamicType type) : base(name, type) { }
public Task(Types type, Dynamic entity) { this.type = type; this.position = new Vec3(float.NaN, float.NaN, float.NaN); this.entityType = null; this.entity = entity; this.wanderRadius = float.NaN; this.wanderDistance = float.NaN; this.wanderJitter = float.NaN; }
public Task(Types type, float wanderRadius, float wanderDistance, float wanderJitter) { this.type = type; this.position = new Vec3(float.NaN, float.NaN, float.NaN); this.entityType = null; this.entity = null; this.wanderRadius = wanderRadius; this.wanderDistance = wanderDistance; this.wanderJitter = wanderJitter; }
public bool UpdateObject(string name, DynamicType value, DistObject o, Int32 timeOut = 0) { return(DistClient_updateObject_name(GetNativeReference(), name, value.GetNativeReference(), o.GetNativeReference(), timeOut)); }