Пример #1
0
        static void Main(string[] args)
        {
            object someObject = GetSomeObject();

            Console.WriteLine($"{Environment.NewLine}Test 1: Valid classic cast with ()");
            // casting the classic way if you 're sure what you expect. We added try/catch to be safe. You never know.
            try
            {
                // Cast will work here
                MyParentClass parent1 = (MyParentClass)someObject;
                Console.WriteLine($"Great, the cast from 'object' to 'MyParentClass' cast did work.");
            }
            catch (InvalidCastException e)
            {
                // we won't get here
                Console.WriteLine($"Ooops. Exception! That's an invalid cast, dude.{Environment.NewLine + e.Message}");
            }

            Console.WriteLine($"{Environment.NewLine}Test 2: Invalid classic cast with ()");
            // invalid cast done in the classic way. Thank god we have that try/catch thing.
            try
            {
                // cast incompatible types
                MyOtherClass other = (MyOtherClass)someObject;

                // we won't see this line
                Console.WriteLine($"Great, the cast from 'object' to 'MyOtherClass' cast did work.");
            }
            catch (InvalidCastException e)
            {
                Console.WriteLine($"Ooops. Exception! That's an invalid cast, dude.[{e.Message}]");
            }

            Console.WriteLine($"{Environment.NewLine}Test 3: Valid cast with 'as'");
            // cast with "as"
            MyParentClass parent2 = someObject as MyParentClass;

            // check if cast was successfull
            if (parent2 != null)
            {
                Console.WriteLine("Cast from object to MyParentClass did work.");
            }


            Console.WriteLine($"{Environment.NewLine}Test 4: Invalid cast with 'as'");
            // invalid cast with "as"
            MyOtherClass other1 = someObject as MyOtherClass;

            // check if cast was UNsuccessfull (mind the == instead of the !=)
            if (other1 == null)
            {
                // This line will be printed.
                Console.WriteLine("Something went wrong. Seems like 'someObject' is not of type 'MyOtherClass'. " +
                                  "However this isn't such a big deal. No Exception here. And we checked for null. Peeew.");
            }



            Console.ReadLine();
        }
Пример #2
0
        public void Method()
        {
            MyClass inst = new MyOtherClass();

            inst.Method(1, 2, 3);
            inst.Method(1, 2);
        }
Пример #3
0
    void Example()
    {
        //Muttable
        var myObject = new MyClass {
            Value = "Value"
        };

        try
        {
            MikValSor.Immutable.EnsuredImmutable <MyClass> myEnsuredObject = MikValSor.Immutable.EnsuredImmutable.Create(myObject);
        }
        catch (MikValSor.Immutable.NotImmutableException)
        {
            System.Console.WriteLine($"myObject is mutable.");
        }

        //Immutable
        var myOtherObject = new MyOtherClass("Value");

        MikValSor.Immutable.EnsuredImmutable <MyOtherClass> myOtherEnsuredObject = MikValSor.Immutable.EnsuredImmutable.Create(myOtherObject);
        System.Console.WriteLine($"myOtherEnsuredObject is immutable.");

        System.Console.WriteLine($"myOtherEnsuredObject.Value: {myOtherEnsuredObject.Value}");

        //Using extensions (using MikValSor.Immutable.Extensions)
        MikValSor.Immutable.EnsuredImmutable <MyOtherClass> myOtherEnsuredObject2 = myOtherObject.EnsureImmutable();
        System.Console.WriteLine($"myOtherEnsuredObject2.Value: {myOtherEnsuredObject2.Value}");
    }
Пример #4
0
 public static string GetText(this MyOtherClass parent)
 {
     using (var helper = parent.CreateHelper())
     {
         return(helper.Text);
     }
 }
    void Validate()
    {
        var validator = new MikValSor.Immutable.ImmutableValidator();

        var myObject = new MyClass {
            Value = "Value"
        };
        bool isMyObjectImmutable = validator.IsImmutable(myObject);

        System.Console.WriteLine($"Is myObject immutable: {isMyObjectImmutable}");

        var  myOtherObject            = new MyOtherClass("Value");
        bool isMyOtherObjectImmutable = validator.IsImmutable(myOtherObject);

        System.Console.WriteLine($"Is myOtherObject immutable: {isMyOtherObjectImmutable}");
    }
Пример #6
0
 public static void ReadValueFromFile(string field, PropertyInfo exampleClassField, ExampleClass record)
 {
     if (exampleClassField.PropertyType.Name == typeof(string).Name)
     {
         record.GetType().InvokeMember(exampleClassField.Name,
                                       BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
                                       Type.DefaultBinder, record, new object[] { field });
     }
     else if (exampleClassField.PropertyType.Name == typeof(double).Name)
     {
         record.GetType().InvokeMember(exampleClassField.Name,
                                       BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
                                       Type.DefaultBinder, record, new object[] { double.Parse(field) });
     }
     else if (exampleClassField.PropertyType.Name == typeof(MyOtherClass).Name)
     {
         var other = new MyOtherClass();
         // TO DO: Parse field to set properties in MyOtherClas
         record.GetType().InvokeMember(exampleClassField.Name,
                                       BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
                                       Type.DefaultBinder, record, new object[] { other });
     }
 }
Пример #7
0
 public static InitializeStatic(int myInt, MyOtherClass other)
 {
     MyInt               = myInt;
     MyOther             = other;
     IsStaticInitialized = true;
 }
Пример #8
0
        static void Main()
        {
            MyOtherClass cls = new MyOtherClass();

            cls.Hello();
        }
Пример #9
0
 static MyClass()
 {
     MyInt   = 12;
     MyOther = new MyOtherClass();
 }
 static async void AsynchrousInitializationPartternMain()
 {
     var myClass = new MyClass();
     var oc      = new MyOtherClass(myClass);
     await oc.InitTask;
 }
Пример #11
0
 public static void Main(string[] args)
 {
     MyClass mc = new MyClass("test");
     MyOtherClass moc = new MyOtherClass();
     Test1.PrintCounterA(mc);
     Test1.PrintCounterB(mc);
     ClassA a = new ClassA();
     ClassB b = new ClassB();
     Test1.PrintCounterA(mc);
     Test1.PrintCounterB(mc);
     Test1.PrintName(mc);
     Test1.PrintName(moc);
     MyClass.DestroyObject(mc);
     Test1.PrintCounterA(mc);
     Test1.PrintCounterB(mc);
     MyClass.DestroyObject(moc);
     mc.PrintCounter(typeof(ClassA));
     mc.PrintCounter(typeof(ClassB));
     mc.PrintCounter(typeof(string));
     mc.Print(2);
     (mc.Create("new MyClass") as MyClass).Print();
     System.Console.Out.WriteLine(mc == ((MyClass)mc.Create("test")));
 }
Пример #12
0
        static void Main(string[] args)
        {
            MyOtherClass cls = new MyOtherClass();

            cls.MyString
        }