Click here to Skip to main content
15,214,392 members
   

C#

 
QuestionRe: Issue while solving 100 Doors Kata Problem Pin
Richard MacCutchan24-Apr-20 22:09
mveRichard MacCutchan24-Apr-20 22:09 
AnswerRe: Issue while solving 100 Doors Kata Problem Pin
Gerry Schmitz25-Apr-20 11:52
mveGerry Schmitz25-Apr-20 11:52 
QuestionSQL Connection in Visual Studio Pin
Member 1481258224-Apr-20 12:25
MemberMember 1481258224-Apr-20 12:25 
AnswerRe: SQL Connection in Visual Studio Pin
Eddy Vluggen24-Apr-20 12:40
professionalEddy Vluggen24-Apr-20 12:40 
AnswerRe: SQL Connection in Visual Studio Pin
Matt Slay24-Apr-20 16:28
MemberMatt Slay24-Apr-20 16:28 
AnswerRe: SQL Connection in Visual Studio Pin
OriginalGriff24-Apr-20 20:09
mveOriginalGriff24-Apr-20 20:09 
AnswerRe: SQL Connection in Visual Studio Pin
Gerry Schmitz25-Apr-20 11:46
mveGerry Schmitz25-Apr-20 11:46 
Questioncasting from interface instance back to generic instance ? Pin
BillWoodruff24-Apr-20 1:11
mveBillWoodruff24-Apr-20 1:11 
the goal: use interfaces to enable creating a collection of different types of objects: and, then, at run-time cast back to the generic type without hard-coding specific types.

a. without using 'dynamic
b. without using reflection
c. without cloning/copying (using something like 'Activator.CreateInstance)

note: you might think you could:

a. use 'Convert.ChangeType : no, it is designed for conversion to a variety of primitive types, requires implementing IConvertible, and it returns an object.

b. use a 'ComponentModel.TypeConverter implementation per class: returns an object; internal method overrides cannot have return type modified

background:

A common pattern in creating heterogenous collections of different Type objects is making the various objects inherit from a generic Interface that itself inherits from a non-generic interface: by casting the instances of the generic interface to the non-generic interface, you then can create a collection you can compile.

when, at run-time, you have an instance of the non-generic interface, you can, then, "upcast" to the generic interface to expose fields, properties, methods unique to the generic type ... but ...

to upcast you need to:

1. know the Type

2. or, use a switch statement in which you use explicitly coded possible type matches

if you have a variable of type 'Type, there is no way to use its run-time value to upcast the non-generic interface instance !

// code example

    public interface IBeing
    {
        string BName { get; set; }
        Type BType { get; set; }
    }

    public interface IBeing<T> : IBeing where T : class
    {
        T Value { get; set; }
    }
    
    
    public class Being<T> : IBeing where T : class
    {
        public Being(string name, T value)
        {
            Value = value;
            BName = name;
        }

        public T Value { get; set; }
        public string BName { get; set; }
        public Type BType { get; set; }
    }
    
 // example classes that implement the interface:
 
     public class Human : IBeing
    {
        public Human(string name)
        {
            BName = name;
        }

        public Type BType { get; set; }

        public string BName { get; set; }
    }

    public class Dog : IBeing
    {
        public Dog(string name)
        {
            BName = name;
        }

        public Type BType { get; set; }

        public string BName { get; set; }
    }
    
   // a collection
    
    public class BeingCache
    {
        public BeingCache()
        {
            Beings = new List<IBeing>();
        }

        public List<IBeing> Beings { get; }

        public IBeing AddBeing<T>(string name, T value) where T : class
        {
            Being<T>  newbeing = new Being<T>(name, value);
            newbeing.BType = typeof(T);
            Beings.Add(newbeing);
            return newbeing;
        }
    }

// usage example

            BeingCache beingCache = new BeingCache();

            Human h1 = new Human("h1");

            Dog d1 = new Dog("d1");
   
            IBeing being1 = beingCache.AddBeing<Human>("Jack", h1);
            IBeing being2 = beingCache.AddBeing<Dog>("Rover", d1);

            IBeing being1human = beingCache.Beings[0];
            IBeing being2dog = beingCache.Beings[1];
At this point, to convert the interface instance back to its generic form: IBeing<Human> backtogeneric1 = (IBeing<Human>) being1Human; will work.

And, a typical pattern is to have a 'switch statement that hard codes the conversion for each interface instancee Type.

But, consider: hard-coded conversion requires design-time "world knowledge" of each Type.

What is desired is a run-time conversion method that uses the Type stored in the interface instance to get the generic interface instance with it "payload" of other data.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali


modified 24-Apr-20 10:50am.

GeneralRe: casting from interface instance back to generic instance ? Pin
harold aptroot24-Apr-20 1:29
Memberharold aptroot24-Apr-20 1:29 
GeneralRe: casting from interface instance back to generic instance ? Pin
BillWoodruff24-Apr-20 2:41
mveBillWoodruff24-Apr-20 2:41 
GeneralRe: casting from interface instance back to generic instance ? Pin
Richard Deeming24-Apr-20 3:26
mveRichard Deeming24-Apr-20 3:26 
GeneralRe: casting from interface instance back to generic instance ? Pin
BillWoodruff24-Apr-20 4:52
mveBillWoodruff24-Apr-20 4:52 
GeneralRe: casting from interface instance back to generic instance ? Pin
Richard Deeming24-Apr-20 4:59
mveRichard Deeming24-Apr-20 4:59 
AnswerRe: casting from interface instance back to generic instance ? Pin
jschell26-Apr-20 9:59
Memberjschell26-Apr-20 9:59 
GeneralRe: During Pin
OriginalGriff23-Apr-20 3:15
mveOriginalGriff23-Apr-20 3:15 
QuestionRefreshing data on a parent form at child close Pin
Uranium-23521-Apr-20 20:24
MemberUranium-23521-Apr-20 20:24 
AnswerRe: Refreshing data on a parent form at child close Pin
OriginalGriff21-Apr-20 21:44
mveOriginalGriff21-Apr-20 21:44 
GeneralRe: Refreshing data on a parent form at child close Pin
Uranium-23521-Apr-20 22:48
MemberUranium-23521-Apr-20 22:48 
GeneralRe: Refreshing data on a parent form at child close Pin
OriginalGriff21-Apr-20 23:04
mveOriginalGriff21-Apr-20 23:04 
GeneralRe: Refreshing data on a parent form at child close Pin
Uranium-23522-Apr-20 23:27
MemberUranium-23522-Apr-20 23:27 
GeneralRe: Refreshing data on a parent form at child close Pin
OriginalGriff23-Apr-20 0:09
mveOriginalGriff23-Apr-20 0:09 
GeneralRe: Refreshing data on a parent form at child close Pin
Uranium-23523-Apr-20 10:46
MemberUranium-23523-Apr-20 10:46 
GeneralRe: Refreshing data on a parent form at child close Pin
OriginalGriff23-Apr-20 11:06
mveOriginalGriff23-Apr-20 11:06 
GeneralRe: Refreshing data on a parent form at child close Pin
Uranium-23524-Apr-20 17:53
MemberUranium-23524-Apr-20 17:53 
GeneralRe: Refreshing data on a parent form at child close Pin
OriginalGriff24-Apr-20 20:08
mveOriginalGriff24-Apr-20 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.