public void MultipleTypesUsingBaseClassMultiSearch() { var data = Enumerable.Range(0, 100) .Select(i => { MyBaseClass o = null; if (i % 2 == 0) { o = new ClassA() { ClassAProperty = Guid.NewGuid().ToString() } } ; else { o = new ClassB() { ClassBProperty = Guid.NewGuid().ToString() } }; o.Title = Guid.NewGuid().ToString(); return(o); }); var resulta = this._client.IndexMany(data.OfType <ClassA>(), new SimpleBulkParameters { Refresh = true }); var resultb = this._client.IndexMany(data.OfType <ClassB>(), new SimpleBulkParameters { Refresh = true }); var queryResults = this._client.MultiSearch(ms => ms .Search <MyBaseClass>("using_types", s => s.AllIndices() .Types(typeof(ClassA), typeof(ClassB)) .From(0) .Size(100) .MatchAll() ) .Search <MyBaseClass>("using_selector", s => s.AllIndices() .ConcreteTypeSelector((o, h) => o.classBProperty != null ? typeof(ClassB) : typeof(ClassA)) .From(0) .Size(100) .MatchAll() ) ); Assert.True(queryResults.IsValid); var firstResult = queryResults.GetResponse <MyBaseClass>("using_types"); Assert.True(firstResult.Documents.Any()); firstResult.Documents.OfType <ClassA>().Any().Should().BeTrue(); firstResult.Documents.OfType <ClassB>().Any().Should().BeTrue(); var secondResult = queryResults.GetResponse <MyBaseClass>("using_selector"); Assert.True(secondResult.Documents.Any()); secondResult.Documents.OfType <ClassA>().Any().Should().BeTrue(); secondResult.Documents.OfType <ClassB>().Any().Should().BeTrue(); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); mybc.Print(); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; Console.WriteLine(derived.MyProperty); Console.WriteLine(mybc.MyProperty); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); // Call Print from derived portion. mybc.Print(); // Call Print from base portion. }
public static void Main() { MyBaseClass b = new MyBaseClass(); MyDerivedClass d = new MyDerivedClass(); Console.WriteLine("Base Method One: {0}", b.MethodOne()); Console.WriteLine("Derived Method One: {0}", d.MethodOne()); }
static void Main() // Main { SecondDerived derived = new SecondDerived(); // Use SecondDerived. MyBaseClass mybc = (MyBaseClass)derived; // Use MyBaseClass. derived.Print(); mybc.Print(); }
public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; //MyBaseClass b = myDerived; var b = myDerived; Console.WriteLine("myBase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); Console.WriteLine("-----------------------------------------"); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.WriteLine("-----------------------------------------"); object[] values = { (int)12, (long)10653, (byte)12, (sbyte)-5, 16.3, "string" }; foreach (var value in values) { Type t = value.GetType(); if (t.Equals(typeof(byte))) { Console.WriteLine("The value: {0} is an unsigned byte.", value); } else if (t.Equals(typeof(sbyte))) { Console.WriteLine("The value: {0} is a signed byte.", value); } else if (t.Equals(typeof(int))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(long))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(double))) { Console.WriteLine("The value: {0} is a double-precision floating point.", value); } else { Console.WriteLine("The value: '{0}' is another data type.", value); } } }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); // Call Print from derived portion. mybc.Print(); // Call Print from base portion. // mybc.var1 = 5; // Error: base class reference cannot // access derived class members. }
public void DoSomeLogicWhichRaisesTheEvent() { if (OnSomethingHappened != null) { MyBaseClass sender = this; var eventArgs = new EventArgs(); //let all subscibers to event know that the event happened OnSomethingHappened(sender, eventArgs); } }
public void GetObjectsTypesasDependencyKeys() { IUnityContainer uc = new UnityContainer() .RegisterType <MyBaseClass, MyClassDerivedBaseClass>(); MyBaseClass mclass = uc.Resolve <MyBaseClass>(); MyClassDerivedBaseClass mder = uc.Resolve <MyClassDerivedBaseClass>(); Assert.IsInstanceOfType(mclass, typeof(MyBaseClass)); Assert.IsInstanceOfType(mder, typeof(MyBaseClass)); }
public void _01() { var derived = new MyDerivedClass(); derived.Print(); // 父类变量指向子类对象, 通过父类变量只能访问父类成员 MyBaseClass mybc = derived; mybc.Print(); }
public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); }
public void _01() { var derived = new MyDerivedClass(); derived.Print(); // 父类变量指向子类对象, 通过父类变量只能访问父类成员, // 但是如果这个成员在父类是标记为 virtual, 在子类是标记为 override, 则此时访问的是子类成员. MyBaseClass mybc = derived; mybc.Print(); }
static void Main(string[] args) { var myBaseClass = new MyBaseClass(); myBaseClass.DoSomeAction(); var myDerivedClass = new MyDerivedClass(); myDerivedClass.DoSomeAction(); ((MyBaseClass)myDerivedClass).DoSomeAction(); myDerivedClass.DoSomeAction(); }
private static DateTime MyMethodOverrider(MyBaseClass vThis) { switch (vThis.MyProperty.ToUpper()) { case "UTC": return DateTime.UtcNow; case "LOCAL": return DateTime.Now; case "FUTURE": return DateTime.Now.AddYears(30); default: return MyMethodBaseCall(vThis); } }
public T Unwrap() { //assert that T has the correct base type if (!typeof(T).IsSubclassOf(typeof(MyBaseClass))) { throw new ArgumentException(); } //must use reflection to construct T obj = (T)typeof(T).InvokeMember(null, BindingFlags.CreateInstance, null, null, null); //cast to a type of MyBaseClass so we can copy our values MyBaseClass c = (MyBaseClass)(object)obj; c.SomeValue = this.SomeValue; return(obj); }
public static void Retrieve(this MyBaseClass entity, int ID) { string className = entity.GetType().Name; Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetStoredProcCommand(String.Format("{0}s_Retrieve_{0}", className)); db.AddInParameter(dbCommand, String.Format("@{0}ID", className), DbType.Int32, ID); using (IDataReader dr = db.ExecuteReader(dbCommand)) { if (dr.Read()) { BOLoader.LoadDataToProps(this, dr); } } }
public void ThrowExceptionIfFieldOrPropetyNotFound() { // # Arrange. var pr = new PseudoRandom(nameof(ThrowExceptionIfFieldOrPropetyNotFound)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); // # Act and Assert. // A setter does not exist. Assert.Throws <Exception>( () => { sut.ThisPropertyOrFieldIsNotFound = pr.Int(); }); // A getter does not exist. Assert.Throws <Exception>( () => { var res = sut.ThisPropertyOrFieldIsNotFound; }); }
public void ThrowExceptionIfMethodNotFound() { // # Arrange. var pr = new PseudoRandom(nameof(ThrowExceptionIfMethodNotFound)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); // # Act and Assert. // The method does not exist. Assert.Throws <Exception>( () => { sut.ThisMethodIsNotFound(pr.Int()); }); // There is a property by the same name. Assert.Throws <RuntimeBinderException>( () => { sut.MyPrivateProperty(pr.Int()); }); }
static void Main(string[] args) { //Console.WriteLine("Hello World!"); //DataProcessing dataProcessing = new DataProcessing(); //dataProcessing.Array(); //dataProcessing.Aggregate(); MyClass myClass = new MyClass("CREATE OBJECT"); Console.WriteLine("myClass created."); for (int i = -1; i <= 0; i++) { try { Console.WriteLine($"\nAttemping to assign {i}to myobj.val..."); myClass.IntVal = i; Console.WriteLine($"Value {myClass.IntVal} assign to myObj.intVal."); } catch (Exception e) { Console.WriteLine($"Exception {e.GetType().FullName}"); Console.WriteLine($"Message:\n{e.Message}"); } } Console.WriteLine($"\nOutputting {myClass.ToString()}"); Console.WriteLine($"output."); Console.WriteLine(MyClass.StaticInt); MyBaseClass myBaseClass = new MyBaseClass(); myBaseClass.Test(); MyDervedClass myDervedClass = new MyDervedClass(); myDervedClass.Test(); TwoSub twoSub = new TwoSub(); twoSub.Test(); _AbstrcatClass _AbstrcatClass = new _AbstrcatClass(); _AbstrcatClass._abstract(); SubClass subClass = new SubClass(); subClass._abstract(); subClass.Adder(); subClass.BaseTest(); subClass.BaseInterFaceTest(); subClass.BaseInterFaceTest1(); }
private static DateTime MyMethodOverrider(MyBaseClass vThis) { switch (vThis.MyProperty.ToUpper()) { case "UTC": return(DateTime.UtcNow); case "LOCAL": return(DateTime.Now); case "FUTURE": return(DateTime.Now.AddYears(30)); default: return(MyMethodBaseCall(vThis)); } }
public void MultipleTypesUsingBaseClass() { var data = Enumerable.Range(0, 100) .Select(i => { MyBaseClass o = null; if (i % 2 == 0) { o = new ClassA() { ClassAProperty = Guid.NewGuid().ToString() } } ; else { o = new ClassB() { ClassBProperty = Guid.NewGuid().ToString() } }; o.Title = Guid.NewGuid().ToString(); return(o); }); var resulta = this._client.IndexMany(data.OfType <ClassA>(), new SimpleBulkParameters { Refresh = true }); var resultb = this._client.IndexMany(data.OfType <ClassB>(), new SimpleBulkParameters { Refresh = true }); var queryResults = this._client.Search <MyBaseClass>(s => s .Types(typeof(ClassA), typeof(ClassB)) .From(0) .Size(100) .MatchAll() ); Assert.True(queryResults.IsValid); Assert.True(queryResults.Documents.Any()); queryResults.Documents.OfType <ClassA>().Any().Should().BeTrue(); queryResults.Documents.OfType <ClassB>().Any().Should().BeTrue(); }
public void SingleIndexWithMultipleTypes() { var data = Enumerable.Range(0, 100) .Select(i => { MyBaseClass o = null; if (i % 2 == 0) { o = new ClassA() { ClassAProperty = Guid.NewGuid().ToString() } } ; else { o = new ClassB() { ClassBProperty = Guid.NewGuid().ToString() } }; o.Title = Guid.NewGuid().ToString(); return(o); }); var result = this._client.IndexMany(data, new SimpleBulkParameters { Refresh = true }); result.IsValid.Should().BeTrue(); result.Items.Count().Should().Be(100); var queryResults = this._client.Search <MyBaseClass>(s => s .From(0) .Size(100) .MatchAll() .ConcreteTypeSelector((o, h) => o.classBProperty != null ? typeof(ClassB) : typeof(ClassA)) ); Assert.True(queryResults.IsValid); Assert.True(queryResults.Documents.Any()); queryResults.Documents.OfType <ClassA>().Any().Should().BeTrue(); queryResults.Documents.OfType <ClassB>().Any().Should().BeTrue(); }
public override int CompareTo(MyBaseClass other) { if (other == null) { return(1); } var typedOther = other as MyBaseClass; if (typedOther != null) { return(this.SomeValue.CompareTo(typedOther.SomeValue)); } else { return(GetType().FullName.CompareTo(other.GetType().FullName)); } }
static void Main(string[] args) { var client = new MyBaseClass <int> .Client(); // add conversion expressions client.List.ReceivingMethod((i) => (i).ToString()); client.List.ReceivingMethod((i) => (2 * i).ToString()); client.List.ReceivingMethod((i) => (3 * i).ToString()); // The programmer has to manually enforce the `string` type // below based on the results of the expressions above. There // is no way to enforce consistency because `TResult` can be // _any_ type. client.Do <string>(); // Produces the following output // // i => i.ToString() // i => (2*i).ToString() // i => (3*i).ToString() }
public void ReachAllMethods() { // # Arrange. var pr = new PseudoRandom(nameof(ReachAllMethods)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); var val = pr.Int(); // # Act and Assert. // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut.MyPrivateMethod(val).Should().Be(val + 1); //sut.MyInternalMethod(val).Should().Be(val + 1); //sut.MyProtectedMethod(val).Should().Be(val + 1); //sut.MyPublicMethod(val).Shoul().Be(val + 1); Assert.Equal(val + 1, sut.MyPrivateMethod(val)); Assert.Equal(val + 1, sut.MyInternalMethod(val)); Assert.Equal(val + 1, sut.MyProtectedMethod(val)); Assert.Equal(val + 1, sut.MyPublicMethod(val)); }
public void ObjAndSutUseTheSameInstance() { // # Arrange. var pr = new PseudoRandom(nameof(ObjAndSutUseTheSameInstance)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); dynamic sut2 = new ReachIn(obj); var value = pr.Int(); value.Should().NotBe(default(int), "Sanity check we haven't randomised the default 0 value since all tests then would be moot."); // # Act. sut.MyPublicProperty = value; sut2.MyPublicProperty = value; // # Assert. value.Should().Be(obj.MyPublicProperty); // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut2.MyPublicProperty.Should().Be(sut.MyPublicProperty); Assert.Equal(sut.MyPublicProperty, sut2.MyPublicProperty); }
public override int CompareTo(MyBaseClass other) { if (other == null) { // every instance comes after null, cf. docs return(1); } var typedOther = other as MyBaseClass; if (typedOther != null) { // other instance of same type; compare by custom sorting criteria return(this.SomeValue.CompareTo(typedOther.SomeValue)); } else { // other instance of different type; make sure different types are always sorted in the same order return(GetType().FullName.CompareTo(other.GetType().FullName)); } }
static void Main(string[] args) { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); // Class Type不會因此被覆蓋 Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.ReadLine(); }
public void MultipleTypesUsingBaseClassMultiSearchSourceInclude() { var data = Enumerable.Range(0, 100) .Select(i => { MyBaseClass o = null; if (i % 2 == 0) { o = new ClassA() { ClassAProperty = Guid.NewGuid().ToString() } } ; else { o = new ClassB() { ClassBProperty = Guid.NewGuid().ToString() } }; o.Title = Guid.NewGuid().ToString(); o.Description = Guid.NewGuid().ToString(); return(o); }); var resulta = this._client.Bulk(b => b.IndexMany(data.OfType <ClassA>()).Refresh()); var resultb = this._client.Bulk(b => b.IndexMany(data.OfType <ClassB>()).Refresh()); var queryResults = this._client.MultiSearch(ms => ms .Search <MyBaseClass>("using_types", s => s.AllIndices() .Types(typeof(ClassA), typeof(ClassB)) .From(0) .Size(100) .Source(source => source .Include(i => i.Add(p => p.Title).Add("classBProperty")) ) .MatchAll() ) .Search <MyBaseClass>("using_selector", s => s.AllIndices() .Types("classa", "classb") .ConcreteTypeSelector((o, h) => o.classBProperty != null ? typeof(ClassB) : typeof(ClassA)) .From(0) .Size(100) .Source(source => source .Include(i => i.Add(p => p.Description).Add("classBProperty")) ) .MatchAll() ) ); Assert.True(queryResults.IsValid); var firstResult = queryResults.GetResponse <MyBaseClass>("using_types"); Assert.True(firstResult.Documents.Any()); firstResult.Documents.All(d => !d.Title.IsNullOrEmpty()); firstResult.Documents.All(d => d.Description.IsNullOrEmpty()); firstResult.Documents.OfType <ClassA>().Any().Should().BeTrue(); firstResult.Documents.OfType <ClassB>().Any().Should().BeTrue(); var secondResult = queryResults.GetResponse <MyBaseClass>("using_selector"); Assert.True(secondResult.Documents.Any()); secondResult.Documents.All(d => d.Title.IsNullOrEmpty()); secondResult.Documents.All(d => !d.Description.IsNullOrEmpty()); secondResult.Documents.OfType <ClassA>().Any().Should().BeTrue(); secondResult.Documents.OfType <ClassB>().Any().Should().BeTrue(); }
private static DateTime MyMethodBaseCall(MyBaseClass vThis) { return vThis.MyMethod(); }
public static bool IsVisibleToPublic(this MyBaseClass obj) { // implementation here }