Click here to Skip to main content
15,886,780 members
Home / Discussions / C#
   

C#

 
QuestionHow to get key board entries on windows explorer ? Pin
srikrishnathanthri30-May-16 20:34
srikrishnathanthri30-May-16 20:34 
AnswerRe: How to get key board entries on windows explorer ? Pin
Dave Kreskowiak31-May-16 3:43
mveDave Kreskowiak31-May-16 3:43 
Questionstore MAC address at installation time Pin
Member 1234275230-May-16 19:45
Member 1234275230-May-16 19:45 
AnswerRe: store MAC address at installation time Pin
OriginalGriff30-May-16 21:04
mveOriginalGriff30-May-16 21:04 
QuestionChange Winform Webbrowser url externally Pin
avisotorisher30-May-16 17:28
avisotorisher30-May-16 17:28 
AnswerRe: Change Winform Webbrowser url externally Pin
Mycroft Holmes30-May-16 18:55
professionalMycroft Holmes30-May-16 18:55 
GeneralRe: Change Winform Webbrowser url externally Pin
avisotorisher30-May-16 19:35
avisotorisher30-May-16 19:35 
Questionstrategy for run-time de-referencing of generic parameter ? Pin
BillWoodruff30-May-16 0:19
professionalBillWoodruff30-May-16 0:19 
edit based on Alan's suggestion below:

What I am looking for is a way to convert the Interface (non-generic Interface) instances ... where the instance is a generic Class ... from the Interface instance to the generic Class instance ... using Reflection, rather than using "world-knowldege" of the specifically defined generic Class parameter Types being used at run-time.

I think, at this point, since that use-case is pretty much what System.Dynamic enables through late-binding that, perhaps, it's better to go ahead snd use System.Dynamic. ... But, I'm not sure. Remember, if I weren't confused, I would not be here Smile | :)

... end edit ...

assuming:

1. an Interface which is not generic
C#
public interface ISomeInterface
{
   int SomeInt { set; get; }
}
2. a Class which is generic which inherits from the Interface
C#
public class SomeClass<T> : ISomeInterface
{
   public int SomeInt { set; get; }

   public T SomeT { set; get; }

   public SomeClass(int anint, T somet = default(T))
   {
      SomeInt = anint;
      SomeT = somet;
   }
}
3. A collection of instances of SomeClass<T> as ISomeInterface
C#
List<ISomeInterface> someInstances = new List<ISomeInterface>();

someInstances.Add(new SomeClass<int>(100, 200));
someInstances.Add(new SomeClass<string>(200, "whoop de doo"));
4. at run-time you have a reference to a member of the 'someInstances list:
C#
ISomeInterface reference = someInstances[1];
5. to detect which generic Type 'reference is:
C#
Type t = reference.GetType().GenericTypeArguments[0];

if (t == typeof(string))
{
    var refAsString = reference as SomeClass<string>;

    string whoopee = refAsString.SomeT;  // => "whoop de doo"
} 
6. is there a better way ... assuming you have a valid need to maintain such a collection of Interface instances and de-reference them into their generic Types at run-time.

Notes:

1. I don't really like doing things this way ... using reflection ... but, I do have a case where either I need to do this, or use System.Dynamic to get away with "Type murder" Smile | :)

2. I am aware of the use of 'Convert.ChangeType, and its limitations, and I believe it is not usable in this case because you need to know what Type to cast to in order to use it. Of course, when your generic parameter is a complex Type, you can't use the 'ChangType method that relies on 'TypeCode.

3. the code example shown here is, of course, simplified for discussion purposes; in practice the generic parameter T may hold references to any object, Class, Struct, etc.

... edit ...

Another way I have experimented with this scenario is to add a Property of Type 'Type to the Interface:
C#
public interface ISomeInterface
{
   int SomeInt { set; get; }
   Type ThisType { set; get; }
}
Which is set in the ctor of the Class:
C#
public SomeClass(int anint, T somet = default(T))
{
   SomeInt = anint;
   SomeT = somet;
   ThisType = typeof(T);
}
Which results in de-referencing code like this:
C#
if (someClassInstance.ThisType == typeof(SomeComplexClass))
{
    var nativeTypedSomeClass = someClassInstance as SomeClass<SomeComplexClass>;
}
That's getting close to the solution that Alan proposed below.

... end edit ...

thanks for your response !
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008


modified 30-May-16 8:37am.

AnswerRe: strategy for run-time de-referencing of generic parameter ? Pin
Alan N30-May-16 1:50
Alan N30-May-16 1:50 
GeneralRe: strategy for run-time de-referencing of generic parameter ? Pin
BillWoodruff30-May-16 2:12
professionalBillWoodruff30-May-16 2:12 
QuestionRe: strategy for run-time de-referencing of generic parameter ? Pin
Richard Deeming1-Jun-16 2:15
mveRichard Deeming1-Jun-16 2:15 
QuestionHow to copy columns data from one excel file to another excel file using C#. Pin
Member 1194327029-May-16 22:00
Member 1194327029-May-16 22:00 
AnswerRe: How to copy columns data from one excel file to another excel file using C#. Pin
Richard MacCutchan29-May-16 22:19
mveRichard MacCutchan29-May-16 22:19 
AnswerRe: How to copy columns data from one excel file to another excel file using C#. Pin
OriginalGriff29-May-16 22:27
mveOriginalGriff29-May-16 22:27 
AnswerRe: How to copy columns data from one excel file to another excel file using C#. Pin
Gerry Schmitz30-May-16 5:43
mveGerry Schmitz30-May-16 5:43 
QuestionHow can i retry read current line at text file? Pin
Member 1243103929-May-16 19:01
Member 1243103929-May-16 19:01 
AnswerRe: How can i retry read current line at text file? Pin
OriginalGriff29-May-16 19:22
mveOriginalGriff29-May-16 19:22 
GeneralRe: How can i retry read current line at text file? Pin
Member 1243103929-May-16 20:16
Member 1243103929-May-16 20:16 
GeneralRe: How can i retry read current line at text file? Pin
OriginalGriff29-May-16 20:53
mveOriginalGriff29-May-16 20:53 
GeneralRe: How can i retry read current line at text file? Pin
Member 1243103929-May-16 22:10
Member 1243103929-May-16 22:10 
GeneralRe: How can i retry read current line at text file? Pin
OriginalGriff29-May-16 22:12
mveOriginalGriff29-May-16 22:12 
GeneralRe: How can i retry read current line at text file? Pin
Matt T Heffron31-May-16 8:52
professionalMatt T Heffron31-May-16 8:52 
AnswerRe: How can i retry read current line at text file? Pin
koolprasad200329-May-16 19:30
professionalkoolprasad200329-May-16 19:30 
GeneralRe: How can i retry read current line at text file? Pin
OriginalGriff29-May-16 20:03
mveOriginalGriff29-May-16 20:03 
GeneralRe: How can i retry read current line at text file? Pin
koolprasad200329-May-16 20:08
professionalkoolprasad200329-May-16 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.