public void ImmediateTypeGetMembers() { TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject(); var immediateType = new ImmediateType(typeof(PublicValueTypeTestClass), TypeAccessor.DefaultFlags | BindingFlags.NonPublic); CollectionAssert.AreEquivalent( classifiedMembers.AllMembers, SelectAllMemberInfos(immediateType.Members)); CollectionAssert.AreEquivalent( classifiedMembers.AllMembers, SelectAllMemberInfos(immediateType.GetMembers())); #region Local function IEnumerable <MemberInfo> SelectAllMemberInfos(IEnumerable <ImmediateMember> members) { return(members.Select <ImmediateMember, MemberInfo>(member => { if (member is ImmediateField field) { return field.FieldInfo; } if (member is ImmediateProperty property) { return property.PropertyInfo; } throw new InvalidOperationException("Members contain an unexpected value"); })); } #endregion }
public void SetPropertyImmediateReflection([NotNull] object obj) { ImmediateType accessor = ImmediateReflection.TypeAccessor.Get(obj.GetType()); ImmediateProperty property = accessor.GetProperty(UIntArrayPropertyName); property?.SetValue(obj, ValueToSet); }
public void TryNewParameterLess() { ConstructorTestHelpers.TryNewParameterLess( (out object instance, out Exception exception) => { var immediateType = new ImmediateType(typeof(ParamsOnlyConstructor)); return(immediateType.TryNew(out instance, out exception)); }, () => new ParamsOnlyConstructor()); ConstructorTestHelpers.TryNewParameterLess( (out object instance, out Exception exception) => { var immediateType = new ImmediateType(typeof(IntParamsOnlyConstructor)); return(immediateType.TryNew(out instance, out exception)); }, () => new IntParamsOnlyConstructor()); ConstructorTestHelpers.TryNewParameterLess( (out object instance, out Exception exception) => { var immediateType = new ImmediateType(typeof(NullableIntParamsOnlyConstructor)); return(immediateType.TryNew(out instance, out exception)); }, () => new NullableIntParamsOnlyConstructor()); }
public void NewParamsOnly() { ConstructorTestHelpers.NewParamsOnly( () => { var immediateType = new ImmediateType(typeof(ParamsOnlyConstructor)); return(immediateType.New()); }, () => new ParamsOnlyConstructor()); ConstructorTestHelpers.NewParamsOnly( () => { var immediateType = new ImmediateType(typeof(IntParamsOnlyConstructor)); return(immediateType.New()); }, () => new IntParamsOnlyConstructor()); ConstructorTestHelpers.NewParamsOnly( () => { var immediateType = new ImmediateType(typeof(NullableIntParamsOnlyConstructor)); return(immediateType.New()); }, () => new NullableIntParamsOnlyConstructor()); }
public void GetWithNonPublic() { TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject(); ImmediateType immediateType = TypeAccessor.Get <PublicValueTypeTestClass>(true); CheckPublicAndNonPublicInstanceMembers(immediateType); immediateType = TypeAccessor.Get(typeof(PublicValueTypeTestClass), true); CheckPublicAndNonPublicInstanceMembers(immediateType); #region Local functions void CheckPublicAndNonPublicInstanceMembers(ImmediateType type) { Assert.IsNotNull(type); Assert.AreEqual(typeof(PublicValueTypeTestClass), type.Type); // Public & Non Public instance members CollectionAssert.AreEquivalent( classifiedMembers.AllFields, immediateType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.NonPublicInstanceProperties).Concat(classifiedMembers.StaticProperties), immediateType.Properties.Select(property => property.PropertyInfo)); } #endregion }
public void GetWithFlags() { TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject(); const BindingFlags flags = BindingFlags.Public | BindingFlags.Static; ImmediateType immediateType = TypeAccessor.Get <PublicValueTypeTestClass>(flags); CheckStaticInstanceMembers(immediateType); immediateType = TypeAccessor.Get(typeof(PublicValueTypeTestClass), flags); CheckStaticInstanceMembers(immediateType); #region Local functions void CheckStaticInstanceMembers(ImmediateType type) { Assert.IsNotNull(type); Assert.AreEqual(typeof(PublicValueTypeTestClass), type.Type); // Static members CollectionAssert.AreEqual( classifiedMembers.StaticFields.Concat(classifiedMembers.ConstFields), immediateType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.StaticProperties, immediateType.Properties.Select(property => property.PropertyInfo)); } #endregion }
public void NewWithParameters_Throws() { // ReSharper disable ReturnValueOfPureMethodIsNotUsed var immediateType = new ImmediateType(typeof(NoDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New(12, 42)); immediateType = new ImmediateType(typeof(NotAccessibleConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New(12)); immediateType = new ImmediateType(typeof(MultiParametersConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New(12f, 12)); immediateType = new ImmediateType(typeof(ParamsConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New(12f, 12)); immediateType = new ImmediateType(typeof(NoDefaultInheritedDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New(12f)); immediateType = new ImmediateType(typeof(AbstractNoConstructor)); Assert.Throws <MemberAccessException>(() => immediateType.New(12)); immediateType = new ImmediateType(typeof(TemplateNoDefaultConstructor <>)); Assert.Throws <ArgumentException>(() => immediateType.New(12)); immediateType = new ImmediateType(typeof(NotDefaultConstructorThrows)); Assert.Throws <TargetInvocationException>(() => immediateType.New(12)); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public void ImmediateTypeNewKeyword() { var immediateType = new ImmediateType(typeof(BaseTestClass)); Assert.AreEqual(typeof(BaseTestClass), immediateType.Type); Assert.AreEqual(nameof(BaseTestClass), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(BaseTestClass)}", immediateType.FullName); CollectionAssert.AreEquivalent( Enumerable.Empty <FieldInfo>(), immediateType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( new[] { BaseClassPublicGetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); immediateType = new ImmediateType(typeof(ChildTestClass)); Assert.AreEqual(typeof(ChildTestClass), immediateType.Type); Assert.AreEqual(nameof(ChildTestClass), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(ChildTestClass)}", immediateType.FullName); CollectionAssert.AreEquivalent( Enumerable.Empty <FieldInfo>(), immediateType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( new[] { ChildClassPublicGetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); }
public object GetPropertyImmediateReflection([NotNull] object obj) { ImmediateType accessor = ImmediateReflection.TypeAccessor.Get(obj.GetType()); ImmediateProperty property = accessor.GetProperty(UIntArrayPropertyName); return(property?.GetValue(obj)); }
public void ImmediateTypeInheritedType() { var immediateType = new ImmediateType(typeof(object)); Assert.AreEqual(typeof(object), immediateType.Type); Assert.IsNull(immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual("Object", immediateType.Name); Assert.AreEqual( $"{nameof(System)}.Object", immediateType.FullName); CollectionAssert.IsEmpty(immediateType.Fields); CollectionAssert.IsEmpty(immediateType.Properties); immediateType = new ImmediateType(typeof(ChildTestClass)); Assert.AreEqual(typeof(ChildTestClass), immediateType.Type); Assert.AreEqual(typeof(BaseTestClass), immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual(nameof(ChildTestClass), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(ChildTestClass)}", immediateType.FullName); CollectionAssert.IsEmpty(immediateType.Fields); CollectionAssert.AreEquivalent( new[] { ChildClassPublicGetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); }
public void ImmediateType_Constructor() { var obj1 = (BenchmarkObject)ImmediateType.New(); var obj2 = (BenchmarkObject2)ImmediateType2.New(); var obj3 = (BenchmarkObject3)ImmediateType3.New(); var obj4 = (BenchmarkObject4)ImmediateType4.New(); }
public void ImmediateTypeInterface() { // Base interface var immediateType = new ImmediateType(typeof(IBaseTestInterface)); Assert.AreEqual(typeof(IBaseTestInterface), immediateType.Type); Assert.IsNull(immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual(nameof(IBaseTestInterface), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(IBaseTestInterface)}", immediateType.FullName); CollectionAssert.IsEmpty(immediateType.Fields); CollectionAssert.AreEquivalent( new[] { BaseInterfaceGetPropertyPropertyInfo, BaseInterfaceSetPropertyPropertyInfo, BaseInterfaceGetSetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); // Child interface immediateType = new ImmediateType(typeof(IChildTestInterface)); Assert.AreEqual(typeof(IChildTestInterface), immediateType.Type); Assert.IsNull(immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual(nameof(IChildTestInterface), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(IChildTestInterface)}", immediateType.FullName); CollectionAssert.IsEmpty(immediateType.Fields); CollectionAssert.AreEquivalent( new[] { ChildInterfaceGetSetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); // Implementation immediateType = new ImmediateType(typeof(ImplementationInterfacesTestClass)); Assert.AreEqual(typeof(ImplementationInterfacesTestClass), immediateType.Type); Assert.AreEqual(typeof(object), immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual(nameof(ImplementationInterfacesTestClass), immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{nameof(ImplementationInterfacesTestClass)}", immediateType.FullName); CollectionAssert.IsEmpty(immediateType.Fields); CollectionAssert.AreEquivalent( new[] { ImplementationBaseInterfaceFromGetPropertyPropertyInfo, ImplementationBaseInterfaceFromSetPropertyPropertyInfo, ImplementationBaseInterfaceFromGetSetPropertyPropertyInfo, ImplementationChildInterfaceFromGetSetPropertyPropertyInfo }, immediateType.Properties.Select(property => property.PropertyInfo)); }
public void ImmediateTypeEnumType() { // Simple test enum CheckEnumType( typeof(TestEnum), new[] { TestEnumFieldValueFieldInfo, TestEnumField1FieldInfo, TestEnumField2FieldInfo }); // Test enum (inherit ulong) CheckEnumType( typeof(TestEnumULong), new[] { TestEnumULongFieldValueFieldInfo, TestEnumULongField1FieldInfo, TestEnumULongField2FieldInfo }); // Flags test enum CheckEnumType( typeof(TestEnumFlags), new[] { TestEnumFlagsFieldValueFieldInfo, TestEnumFlagsField1FieldInfo, TestEnumFlagsField2FieldInfo, TestEnumFlagsField3FieldInfo }); #region Local functions void CheckEnumType(Type enumType, IEnumerable <FieldInfo> enumFields) { var immediateType = new ImmediateType(enumType); Assert.AreEqual(enumType, immediateType.Type); Assert.AreEqual(typeof(Enum), immediateType.BaseType); Assert.IsNull(immediateType.DeclaringType); Assert.AreEqual(enumType.Name, immediateType.Name); Assert.AreEqual( $"{nameof(ImmediateReflection)}.{nameof(Tests)}.{enumType.Name}", immediateType.FullName); CollectionAssert.AreEquivalent( enumFields, immediateType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( Enumerable.Empty <PropertyInfo>(), immediateType.Properties.Select(property => property.PropertyInfo)); } #endregion }
public void NewParameterLess([NotNull] Type type) { ConstructorTestHelpers.NewParameterLess( type, () => { var immediateType = new ImmediateType(type); return(immediateType.New()); }); }
public void ImmediateTypeToString() { var immediateType1 = new ImmediateType(typeof(PublicValueTypeTestClass)); Assert.AreEqual(typeof(PublicValueTypeTestClass).ToString(), immediateType1.ToString()); var immediateType2 = new ImmediateType(typeof(InternalValueTypeTestClass)); Assert.AreNotEqual(immediateType1.ToString(), immediateType2.ToString()); }
public void ImmediateTypeGetProperties() { var immediateType1 = new ImmediateType(typeof(PublicValueTypeTestClass)); CollectionAssert.AreEquivalent(immediateType1.Properties, immediateType1.GetProperties()); var immediateType2 = new ImmediateType(typeof(PublicReferenceTypeTestClass)); CollectionAssert.AreNotEquivalent(immediateType1.GetProperties(), immediateType2.GetProperties()); }
public void Copy([NotNull] Type type, [CanBeNull] object other) { ConstructorTestHelpers.Copy( type, other, o => { var immediateType = new ImmediateType(type); return(immediateType.Copy(o)); }); }
public void NewWithParameters([NotNull] Type type, [CanBeNull, ItemCanBeNull] params object[] arguments) { ConstructorTestHelpers.NewWithParameters( type, args => { var immediateType = new ImmediateType(type); return(immediateType.New(args)); }, arguments); }
public void TryNewParameterLess([NotNull] Type type, bool expectFail) { ConstructorTestHelpers.TryNewParameterLess( type, expectFail, (out object instance, out Exception exception) => { var immediateType = new ImmediateType(type); return(immediateType.TryNew(out instance, out exception)); }); }
public void TryNewWithParameters([NotNull] Type type, bool expectFail, [CanBeNull, ItemCanBeNull] params object[] arguments) { ConstructorTestHelpers.TryNewWithParameters( type, expectFail, (out object instance, out Exception exception, object[] args) => { var immediateType = new ImmediateType(type); return(immediateType.TryNew(out instance, out exception, args)); }, arguments); }
public void GetCached() { ImmediateType immediateType1 = TypeAccessor.Get(typeof(PublicValueTypeTestClass)); Assert.IsNotNull(immediateType1); ImmediateType immediateType2 = TypeAccessor.Get <PublicValueTypeTestClass>(); Assert.IsNotNull(immediateType2); Assert.AreSame(immediateType1, immediateType2); }
public static string ImmediateOperate(string ip, int port, ImmediateType type, string path, string exepara) { // 通过Socket处理 string args = ((int)type).ToString() + "\n" + path + "\n" + exepara; string str = SendToServer(ip, port, OperationType.Immediate, args); //if (str.StartsWith("err", StringComparison.OrdinalIgnoreCase)) //{ // return str.Substring(3); //} return(str); }
public void ImmediateTypeHashCode() { var immediateType1 = new ImmediateType(typeof(PublicValueTypeTestClass)); var immediateType2 = new ImmediateType(typeof(PublicValueTypeTestClass)); Assert.AreEqual(typeof(PublicValueTypeTestClass).GetHashCode(), immediateType1.GetHashCode()); Assert.AreEqual(immediateType1.GetHashCode(), immediateType2.GetHashCode()); var immediateType3 = new ImmediateType(typeof(InternalValueTypeTestClass)); Assert.AreNotEqual(immediateType1.GetHashCode(), immediateType3.GetHashCode()); }
public void TryCopy([NotNull] Type type, [CanBeNull] object other, bool expectFail) { ConstructorTestHelpers.TryCopy( type, other, expectFail, (object o, out object instance, out Exception exception) => { var immediateType = new ImmediateType(type); return(immediateType.TryCopy(o, out instance, out exception)); }); }
public void ImmediateTypeGetProperty() { var immediateType = new ImmediateType(typeof(PublicValueTypeTestClass)); string propertyName = nameof(PublicValueTypeTestClass.PublicPropertyGetSet); Assert.AreEqual(immediateType.Properties[propertyName], immediateType.GetProperty(propertyName)); propertyName = "NotExists"; Assert.IsNull(immediateType.GetProperty(propertyName)); // ReSharper disable once AssignNullToNotNullAttribute // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Assert.Throws <ArgumentNullException>(() => immediateType.GetProperty(null)); }
public void ImmediateTypeGetField() { var immediateType = new ImmediateType(typeof(PublicValueTypeTestClass)); string fieldName = nameof(PublicValueTypeTestClass._publicField); Assert.AreEqual(immediateType.Fields[fieldName], immediateType.GetField(fieldName)); fieldName = "NotExists"; Assert.IsNull(immediateType.GetField(fieldName)); // ReSharper disable once AssignNullToNotNullAttribute // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Assert.Throws <ArgumentNullException>(() => immediateType.GetField(null)); }
public void ImmediateTypeEquality() { var immediateType1 = new ImmediateType(typeof(PublicValueTypeTestClass)); var immediateType2 = new ImmediateType(typeof(PublicValueTypeTestClass)); Assert.IsTrue(immediateType1.Equals(immediateType1)); Assert.IsTrue(immediateType1.Equals(immediateType2)); Assert.IsTrue(immediateType1.Equals((object)immediateType2)); Assert.IsFalse(immediateType1.Equals(null)); var immediateType3 = new ImmediateType(typeof(InternalValueTypeTestClass)); Assert.IsFalse(immediateType1.Equals(immediateType3)); Assert.IsFalse(immediateType1.Equals((object)immediateType3)); }
private ABCOpInfo( ImmediateType opType = ImmediateType.NONE, ControlType controlType = ControlType.NONE, int stackPopCount = 0, int stackPushCount = 0, bool pushesToScopeStack = false, bool popsFromScopeStack = false, bool localRead = false, bool localWrite = false, bool isDebug = false ) { int data = VALID_FLAG; data |= (int)opType; data |= (int)controlType << 3; if (stackPopCount == -1) { stackPopCount = 3; } data |= stackPopCount << 6; data |= stackPushCount << 8; if (pushesToScopeStack) { data |= SCOPE_PUSH_FLAG; } if (popsFromScopeStack) { data |= SCOPE_POP_FLAG; } if (localRead) { data |= LOCAL_READ_FLAG; } if (localWrite) { data |= LOCAL_WRITE_FLAG; } if (isDebug) { data |= DEBUG_FLAG; } m_data = (short)data; }
public void NewParameterLess_Throws() { // ReSharper disable ReturnValueOfPureMethodIsNotUsed var immediateType = new ImmediateType(typeof(NoDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(NotAccessibleDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(IList <int>)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(IDictionary <int, string>)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(AbstractDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(StaticClass)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(TemplateStruct <>)); Assert.Throws <ArgumentException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(TemplateDefaultConstructor <>)); Assert.Throws <ArgumentException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(ParamsConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(NoDefaultInheritedDefaultConstructor)); Assert.Throws <MissingMethodException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(AmbiguousParamsOnlyConstructor)); Assert.Throws <AmbiguousMatchException>(() => immediateType.New()); // ReSharper disable once PossibleMistakenCallToGetType.2 immediateType = new ImmediateType(typeof(DefaultConstructor).GetType()); Assert.Throws <ArgumentException>(() => immediateType.New()); immediateType = new ImmediateType(typeof(DefaultConstructorThrows)); Assert.Throws(Is.InstanceOf <Exception>(), () => immediateType.New()); immediateType = new ImmediateType(typeof(int[])); Assert.Throws <MissingMethodException>(() => immediateType.New()); // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public void ImmediateTypeWithFlags() { TypeClassifiedMembers classifiedMembers = TypeClassifiedMembers.GetForPublicValueTypeTestObject(); var testType = new ImmediateType(typeof(PublicValueTypeTestClass)); // BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static CollectionAssert.AreEqual( classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.StaticFields).Concat(classifiedMembers.ConstFields), testType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.StaticProperties), testType.Properties.Select(property => property.PropertyInfo)); testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.NonPublic | BindingFlags.Instance); CollectionAssert.AreEqual( classifiedMembers.NonPublicInstanceFields, testType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.NonPublicInstanceProperties, testType.Properties.Select(property => property.PropertyInfo)); testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); CollectionAssert.AreEqual( classifiedMembers.PublicInstanceFields.Concat(classifiedMembers.NonPublicInstanceFields), testType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.PublicInstanceProperties.Concat(classifiedMembers.NonPublicInstanceProperties), testType.Properties.Select(property => property.PropertyInfo)); testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); CollectionAssert.AreEqual( classifiedMembers.StaticFields.Concat(classifiedMembers.ConstFields), testType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEquivalent( classifiedMembers.StaticProperties, testType.Properties.Select(property => property.PropertyInfo)); testType = new ImmediateType(typeof(PublicValueTypeTestClass), BindingFlags.IgnoreCase); CollectionAssert.AreEqual( Enumerable.Empty <FieldInfo>(), testType.Fields.Select(field => field.FieldInfo)); CollectionAssert.AreEqual( Enumerable.Empty <PropertyInfo>(), testType.Properties.Select(property => property.PropertyInfo)); }