Click here to Skip to main content
15,895,709 members
Home / Discussions / C#
   

C#

 
GeneralRe: Zlib Pin
Scott Dorman28-Sep-07 7:33
professionalScott Dorman28-Sep-07 7:33 
GeneralRe: Zlib Pin
Luc Pattyn28-Sep-07 11:56
sitebuilderLuc Pattyn28-Sep-07 11:56 
GeneralRe: Zlib Pin
Xmen Real 28-Sep-07 14:05
professional Xmen Real 28-Sep-07 14:05 
GeneralRe: Zlib Pin
Scott Dorman28-Sep-07 17:31
professionalScott Dorman28-Sep-07 17:31 
GeneralRe: Zlib Pin
Xmen Real 28-Sep-07 17:55
professional Xmen Real 28-Sep-07 17:55 
QuestionGetGenericArguments() on non-generic subclasses of arbitrary generic types? Pin
Yellowseed28-Sep-07 6:16
Yellowseed28-Sep-07 6:16 
AnswerRe: GetGenericArguments() on non-generic subclasses of arbitrary generic types? Pin
Scott Dorman28-Sep-07 7:22
professionalScott Dorman28-Sep-07 7:22 
GeneralRe: GetGenericArguments() on non-generic subclasses of arbitrary generic types? Pin
Yellowseed28-Sep-07 8:52
Yellowseed28-Sep-07 8:52 
I'm trying to determine if it implements ICollection<>, not a specific type like ICollection<int>.

IsAssignableFrom is a nice magical method, but it only works for specific types.

Anyways, I think I found the right way to do what I want (after, of course, putting my question in a public space...). Thanks for your help. Here's my solution, and the problem stated in sharper terms:

Supposing type X as follows,

class X
{
private class WackyClass<T> : ICollection<T>, IList<int> { }
private class XList : WackyClass<string> { }
static public readonly object Data = new XList();
}

Implement this method, which has no access to X's private types:

/// <example><code>
/// Type[][] args = GetGenericArgs(X.Data, typeof(ICollection<>));
/// Assert(args == new Type[][] {new Type[] {typeof(string)}, new Type[] {typeof(int)}});
/// </code></example>
Type[][] GetGenericArgs(Type subType, Type genericType);

Here is an implementation I feel much better about

if (genericType.IsGenericTypeDefinition == false)
if (genericType.IsGenericType && genericType.IsAssignableFrom(subType))
return new Type[][] { genericType.GetGenericArguments() };
else
return new Type[][] { };
else
{
IEnumerable<Type> candidates;
if (genericType.IsInterface)
candidates = subType.GetInterfaces();
else
candidates = Iterators.Traverse<Type>(subType, "BaseType");
List<Type[]> result = new List<Type[]>();
foreach (Type candidate in candidates)
if (candidate.IsGenericType && candidate.GetGenericTypeDefinition() == genericType)
result.Add(candidate.GetGenericArguments());
return result.ToArray();
}
QuestionDisplaying a messagebox Pin
Albu Marius28-Sep-07 6:02
Albu Marius28-Sep-07 6:02 
AnswerRe: Displaying a messagebox Pin
TJoe28-Sep-07 6:26
TJoe28-Sep-07 6:26 
QuestionWriting To A Text File Pin
rowdykuttan28-Sep-07 5:21
rowdykuttan28-Sep-07 5:21 
AnswerRe: Writing To A Text File Pin
Colin Angus Mackay28-Sep-07 5:32
Colin Angus Mackay28-Sep-07 5:32 
GeneralRe: Writing To A Text File Pin
rowdykuttan28-Sep-07 5:36
rowdykuttan28-Sep-07 5:36 
GeneralRe: Writing To A Text File Pin
Colin Angus Mackay28-Sep-07 5:43
Colin Angus Mackay28-Sep-07 5:43 
AnswerRe: Writing To A Text File Pin
Matthew Cuba28-Sep-07 5:40
Matthew Cuba28-Sep-07 5:40 
AnswerRe: Writing To A Text File Pin
PIEBALDconsult28-Sep-07 6:07
mvePIEBALDconsult28-Sep-07 6:07 
GeneralRe: Writing To A Text File Pin
rowdykuttan28-Sep-07 6:39
rowdykuttan28-Sep-07 6:39 
GeneralRe: Writing To A Text File Pin
PIEBALDconsult28-Sep-07 7:15
mvePIEBALDconsult28-Sep-07 7:15 
GeneralRe: Writing To A Text File Pin
rowdykuttan28-Sep-07 8:03
rowdykuttan28-Sep-07 8:03 
GeneralRe: Writing To A Text File Pin
PIEBALDconsult28-Sep-07 8:16
mvePIEBALDconsult28-Sep-07 8:16 
GeneralRe: Writing To A Text File Pin
rowdykuttan28-Sep-07 8:34
rowdykuttan28-Sep-07 8:34 
GeneralRe: Writing To A Text File Pin
PIEBALDconsult28-Sep-07 8:43
mvePIEBALDconsult28-Sep-07 8:43 
QuestionMy tables hate me Pin
Evan St. John28-Sep-07 4:53
Evan St. John28-Sep-07 4:53 
AnswerRe: My tables hate me Pin
Colin Angus Mackay28-Sep-07 5:19
Colin Angus Mackay28-Sep-07 5:19 
GeneralRe: My tables hate me Pin
BoneSoft28-Sep-07 5:22
BoneSoft28-Sep-07 5:22 

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.