static void Main(string[] args) { DerivedClass instance = new DerivedClass(); instance.Method(); instance.Method1(); instance.Method2(); Console.WriteLine(new string('-', 55)); BaseClass instance0 = instance as BaseClass; //видно только методы класса BaseClass instance0.Method(); IInterface1 instance1 = instance as IInterface1; //видно только методы интерфейса IInterface1 instance1.Method1(); IInterface2 instance2 = instance as IInterface2; //видно только методы интерфейса IInterface2 instance2.Method2(); //Delay Console.ReadKey(); }
static void Main(string[] args) { DerivedClass instance = new DerivedClass(); instance.Method(); instance.Method1(); instance.Method2(); Console.WriteLine(new string('-', 30)); BaseClass instanse1 = instance as BaseClass; instanse1.Method(); //Другие два метода недоступны IInterface1 instance2 = instance as IInterface1; instance2.Method1(); //Другие два метода недоступны IInterface2 instance3 = instance2 as IInterface2; instance3.Method2(); //Другие два метода недоступны IInterface2 instance4 = instanse1 as IInterface2; instance4.Method2(); Console.WriteLine(new string('-', 30)); BaseClass inst = new BaseClass(); IInterface2 inst1 = inst as IInterface2; //А вот это недопустимо, потому что даункаст без апкаста до этого? inst1.Method2(); }
public void test_Resolve_WhenCalledForRegisteredInterface_ReturnsExpectedConcreteType() { systemUnderTest.Register <IInterface2, Class2>(); IInterface2 result = systemUnderTest.Resolve <IInterface2>(); Assert.That(result, Is.InstanceOf <Class2>()); }
public IInterface2 sample_display() //sample_display() is a instance method for Interface { IInterface2 inter_object = null; //assigning NULL for the interface object inter_object = new Class1(); //initialising the interface object to the class1() return(inter_object); //returning the interface object }
public void Test() { Entity e = (Entity)TypeAccessor.CreateInstance(typeof(Entity)); Test2.IInterface1 t2a = (Test2.IInterface1)e; t2a.DoAction(); // because of boxing :( Assert.AreEqual(null /*"Test2.IAction1.DoAction"*/, e.Str); Test1.IInterface1 t1a = (Test1.IInterface1)e; t1a.DoAction(); Assert.AreEqual("Test1.IAction1.DoAction", e.Str); IInterface2 a2 = (IInterface2)e; a2.DoAction(); Assert.AreEqual(123, e.Int); Assert.AreEqual(MyDateTime.TestDate1, e.Date); a2.DoAction(456, null); Assert.AreEqual(456, e.Int); Assert.AreEqual(MyDateTime.TestDate2, e.Date); Assert.AreEqual(2, e.CallCounter); ISetInfo si = (ISetInfo)e; si.SetInfo(1, null, 2); Assert.AreEqual("Str", e.Str); }
public void test_Resolve_WhenCalledTwiceForInterfaceWithLifeStyleTypeSingleton_ReturnsSameObject() { systemUnderTest.Register <IInterface2, Class2>(LifeStyleType.Singleton); IInterface2 result1 = systemUnderTest.Resolve <IInterface2>(); IInterface2 result2 = systemUnderTest.Resolve <IInterface2>(); Assert.That(result1 == result2, Is.True); }
public void test_Resolve_WhenCalledTwiceForInterfaceWithLifeStyleTypeTransient_ReturnsDifferentObjects() { systemUnderTest.Register <IInterface2, Class2>(); IInterface2 result1 = systemUnderTest.Resolve <IInterface2>(); IInterface2 result2 = systemUnderTest.Resolve <IInterface2>(); Assert.That(result1 == result2, Is.False); }
public Class1(IInterface2 argument2, IInterface4 argument4, IInterface5 argument5, IInterface6 argument6, IInterface20 argument20) { Argument2 = argument2; Argument4 = argument4; Argument5 = argument5; Argument6 = argument6; Argument20 = argument20; }
static void Main(string[] args) { TestClass test = new TestClass(); IInterface1 i1 = test; i1.SameMethod(); // will call IInterface1.SameMethod() IInterface2 i2 = test; i2.SameMethod(); // will call IInterface2.SameMethod() }
public void ItShouldResolveInterface2() { // Act IInterface2 actual = _container.GetService <IInterface2>(); // Assert actual .Should() .NotBeNull() .And.BeOfType <ConcreteDerivedClass>(); }
static void Main(string[] args) { DerivedClass derivedClass = new DerivedClass(); derivedClass.Method1(); derivedClass.Method2(); derivedClass.Method3(); IInterface2 derivedClassUpToI2 = derivedClass; derivedClassUpToI2.Method1(); derivedClassUpToI2.Method2(); }
public void Minimal() { IContainer container = new OdysseyContainer(new List <Registration> { new Registration(typeof(IInterface), typeof(Implementation)), new Registration(typeof(IInterface2), typeof(Implementation2)), }); IInterface2 service = (IInterface2)container.Resolve(new Resolution(typeof(IInterface2))); Assert.IsNotNull(service); Assert.IsNotNull(service.Inter); Assert.IsInstanceOfType(service.Inter, typeof(IInterface)); }
static void Main(string[] args) { MyClass my = new MyClass(); my.M(); IInterface1 i1 = my; i1.M(); IInterface2 i2 = my; i2.M(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); IInterface1 instance1 = instance as IInterface1; instance1.Method(); IInterface2 instance2 = instance as IInterface2; instance2.Method(); Console.ReadKey(); }
static void Main(string[] args) { DerivedClass instance = new DerivedClass(); //instance. - не видит методов интерфейса //Для этого апкастим к базовому интерфейсному типу IInterface1 instance1 = instance as IInterface1; instance1.Method(); IInterface2 instance2 = instance as IInterface2; instance2.Method(); }
static void Main(string[] args) { Program p = new Program(); p.Sub(20, 10); p.Add(10, 20); IInterface2 i = p; i.Add(10, 10); i.Sub(20, 10); }
static void Main(string[] args) { DerivedClass dClass = new DerivedClass(); dClass.Method1(); dClass.Method2(); dClass.Method3(); IInterface2 dClassUp = dClass; dClassUp.Method1(); dClassUp.Method2(); Console.ReadKey(); }
static void Main() { ConcreteClass instance = new ConcreteClass(); //instance.Method (); IInterface1 instance1 = instance as IInterface1; instance1.Method(); IInterface2 instance2 = instance as IInterface2; instance2.Method(); // Delay. Console.ReadKey(); }
static void Main(string[] args) { TestInterface te = new TestInterface(); te.Test(); IInterface1 i1 = te; i1.Show(); IInterface2 i2 = te; i2.Show(); Console.ReadLine(); }
public void InterfacesTest() { Implementation imp = new Implementation(); IInterface1 i1 = imp; IInterface2 i2 = imp; Console.WriteLine("A call from implementation directly"); imp.DoSmth(); Console.WriteLine("A call from interface1 "); i1.DoSmth(); Console.WriteLine("A call from interface2 "); i2.DoSmth(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); // instance. ----Ничего не видно IInterface1 instance1 = instance as IInterface1; instance1.Method();//один метод //ОН ВИДЕН ПОТОМУ, ЧТО ВСЕ МЕТОДЫ В ИНТЕРФЕЙСАХ ПО УМОЛЧАНИЮ PUBLIC!!! IInterface2 instance2 = instance as IInterface2; instance2.Method(); instance2.Method();//другой метод //ОН ВИДЕН ПОТОМУ, ЧТО ВСЕ МЕТОДЫ В ИНТЕРФЕЙСАХ ПО УМОЛЧАНИЮ PUBLIC!!! //Delay Console.ReadKey(); }
static void Main(string[] args) { DerivedClass instance = new DerivedClass(); // instance. -- //На экземпляре не видим private методов интерфейсов! //Приведем экземпляр класса DerivedClasss - instance, к базовому интервейсному типу IInterface1 IInterface1 instance1 = instance as IInterface1; instance1.Method(); IInterface2 interface2 = instance as IInterface2; interface2.Method(); //Но мы можем увидить методы, которые public instance.Method3(); //Delay Console.ReadKey(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance1 = instance as IInterface1; instance1.Method1(); IInterface2 instance2 = instance as IInterface2; //тут у нас есть доступ и к Method1 и к Method2 instance2.Method1(); // т.к IInterface2 наследуется от IInterface1 instance2.Method2(); //Delay Console.ReadKey(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance2 = instance as IInterface1; instance2.Method1(); IInterface2 instance3 = instance2 as IInterface2; instance3.Method1(); instance3.Method2(); Console.WriteLine(new string('-', 30)); ConcreteClass concreteClass = new ConcreteClass(); IInterface2 interface2 = concreteClass as IInterface2; interface2.Method1(); interface2.Method2(); }
public ClassWithCircularDependencyThroughInterfaces1(IInterface2 dep2) { }
public Impl1([TestContract("b1")] IInterface2 s1, [TestContract("b2")] IInterface2 s2) { this.s1 = s1; this.s2 = s2; }
public Class3(IInterface1 p1, IInterface2 p2, int p3) {}
public DependentImplementation(IInterface2 _) { }
public ClassWithDependencies(IInterface iInterface, IInterface2 interface2) { Interface = iInterface; Interface2 = interface2; }
public DerivedForInterface2(IInterface2 instance) { _instance = instance; }
public Class4(IInterface1 p1, IInterface2 p2) {}
static void Main(string[] args) { #region 5 лабораторная //Console.WriteLine("Lab5"); Sea sea1 = new Sea("Baltic"); Sea sea2 = new Sea("Nordic"); Sea[] seas1 = { sea1, sea2 }; Ocean ocean1 = new Ocean("Atlantic", seas1); Sea sea3 = new Sea("White"); Sea sea4 = new Sea("Norwegian"); Sea[] seas2 = { sea3, sea4 }; Ocean ocean2 = new Ocean("Arctic", seas2); Ocean[] oceans = { ocean1, ocean2 }; Water water = new Water(oceans, ""); Country country1 = new Country("England"); Country country2 = new Country("Germany"); Country[] countrys1 = { country1, country2 }; Continent continent1 = new Continent("Europe", countrys1); Country country3 = new Country("USA"); Country country4 = new Country("Canada"); Country[] countrys2 = { country3, country4 }; Continent continent2 = new Continent("North America", countrys2); Continent[] continents = { continent1, continent2 }; Island island1 = new Island("Sumatra"); Island island2 = new Island("Schpicbergen"); Island island3 = new Island("Corsika"); Island[] islands = { island1, island2, island3 }; Land land = new Land(continents, islands, "Earth"); Realization real = new Realization(); //real.WriteAbstract(); //real.WriteInterfaceMethod(); IInterface1 i1 = new Realization(); //i1.WriteInterfaceMethod(); IInterface2 i2 = new Realization(); //i2.WriteInterfaceMethod(); AbstractClass a1 = new Realization(); //a1.WriteInterfaceMethod(); //((IInterface1)real).WriteInterfaceMethod(); //((IInterface2)real).WriteInterfaceMethod(); AbstractClass abstr1 = real as Realization; //abstr1.WriteInterfaceMethod(); IInterface1 i11 = real as Realization; IInterface2 i22 = real as Realization; //i11.WriteInterfaceMethod(); //i22.WriteInterfaceMethod(); //Console.WriteLine(Printer.IAmPrinting(i1)); //Console.WriteLine(Printer.IAmPrinting(i11)); //Console.WriteLine(); #endregion try { // Console.WriteLine("Lab6"); Planet Earth = new Planet(water, land, "Earth"); BinaryFormatter bf = new BinaryFormatter(); string path; using (FileStream stream = new FileStream(path = "D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\14.bin", FileMode.Create)) { bf.Serialize(stream, Earth); } using (FileStream stream = new FileStream(path, FileMode.Open)) { Planet planet = (Planet)bf.Deserialize(stream); Console.WriteLine("Binary:\n\tName: " + planet.Name); } SoapFormatter sf = new SoapFormatter(); using (FileStream stream = new FileStream(path = "D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\14.soap", FileMode.Create)) { sf.Serialize(stream, Earth); } using (FileStream stream = new FileStream(path, FileMode.Open)) { Planet planet = (Planet)sf.Deserialize(stream); Console.WriteLine("Soap:\n\tName: " + planet.Name); } DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(Planet)); using (FileStream stream = new FileStream(path = "D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\14.json", FileMode.Create)) { jsonFormatter.WriteObject(stream, Earth); } using (FileStream stream = new FileStream(path, FileMode.Open)) { Planet planet = (Planet)jsonFormatter.ReadObject(stream); Console.WriteLine("JSON:\n\tName: " + planet.Name); } XmlSerializer xs = new XmlSerializer(typeof(Planet)); using (FileStream stream = new FileStream(path = "D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\14.xml", FileMode.Create)) { xs.Serialize(stream, Earth); } using (FileStream stream = new FileStream(path, FileMode.Open)) { Planet planet = (Planet)xs.Deserialize(stream); Console.WriteLine("Xml:\n\tName: " + planet.Name); } Planet Mars = new Planet(water, land, "Mars"); Planet Yupiter = new Planet(water, land, "Yupiter"); Planet Mercury = new Planet(water, land, "Mercury"); Earth.size = 2; Mars.size = 2; Yupiter.size = 3; Mercury.size = 1; Planet[] SunSystem = { Earth, Mars, Yupiter, Mercury }; XmlSerializer xmls = new XmlSerializer(typeof(Planet[])); using (FileStream stream = new FileStream(path = "D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\14Task2.xml", FileMode.Create)) { xmls.Serialize(stream, SunSystem); } using (FileStream stream = new FileStream(path, FileMode.Open)) { Console.WriteLine("Xml task2"); Planet[] planets = (Planet[])xmls.Deserialize(stream); foreach (Planet planet in planets) { Console.WriteLine("\tName: " + planet.Name); } } XmlDocument xd = new XmlDocument(); xd.Load(path); XmlElement xr = xd.DocumentElement; Console.WriteLine("\nName - Earth: "); XmlNode childnode = xr.SelectSingleNode("Planet[Name='Earth']"); if (childnode != null) { Console.WriteLine(childnode.OuterXml); } Console.WriteLine("\nsize = 2: "); XmlNodeList childnodes = xr.SelectNodes("Planet[size='2']"); foreach (XmlNode n in childnodes) { Console.WriteLine(n.OuterXml); } Console.WriteLine("\nLINQ to XML"); XDocument xdoc = new XDocument(); // создаем первый элемент XElement team = new XElement("team"); // создаем атрибут XAttribute team_name_attr = new XAttribute("name", "Arsenal"); XElement team_league_elem = new XElement("league", "BPL"); XElement team_coach_elem = new XElement("coach", "Arsen Wenger"); // добавляем атрибут и элементы в первый элемент team.Add(team_name_attr); team.Add(team_league_elem); team.Add(team_coach_elem); // создаем второй элемент XElement team2 = new XElement("team"); XAttribute team2_name_attr = new XAttribute("name", "Morecambe"); XElement team2_league_elem = new XElement("league", "League One"); XElement team2_coach_elem = new XElement("coach", "Andrei Chayeuski"); // добавляем атрибут и элементы во второй элемент team2.Add(team2_name_attr); team2.Add(team2_league_elem); team2.Add(team2_coach_elem); // создаем корневой элемент XElement teams = new XElement("teams"); // добавляем в корневой элемент teams.Add(team); teams.Add(team2); // добавляем корневой элемент в документ xdoc.Add(teams); //сохраняем документ xdoc.Save("D:\\Документы\\Университет\\3 семестр\\ООТП\\Лабораторные\\Lab14\\Lab14\\bin\\Debug\\teams.xml"); var items = from xe in xdoc.Elements("teams").Elements("team") where xe.Element("league").Value == "BPL" select new Team { Name = xe.Attribute("name").Value, Coach = xe.Element("coach").Value }; foreach (var item in items) { Console.WriteLine("\t{0} - {1}", item.Name, item.Coach); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }
public ClassWithSimpleDependencies(IInterface1 dependency1, IInterface2 dependency2) { Dependency1 = dependency1; Dependency2 = dependency2; }
public CircularReference1(IInterface2 arg) { }
public Class1WithProperty(IInterface2 arg) { Arg = arg; }