Click here to Skip to main content
15,884,177 members
Home / Discussions / C#
   

C#

 
GeneralRe: What are your suggestions ? Pin
Mohammad Dayyan5-Jul-08 16:38
Mohammad Dayyan5-Jul-08 16:38 
QuestionRichText Appending in c#.net Pin
SHINOJK5-Jul-08 7:44
SHINOJK5-Jul-08 7:44 
AnswerRe: RichText Appending in c#.net Pin
hpjchobbes5-Jul-08 8:02
hpjchobbes5-Jul-08 8:02 
QuestionCalling a generic method with a typeof expression [modified] Pin
Lutosław5-Jul-08 7:34
Lutosław5-Jul-08 7:34 
AnswerRe: Initiating a generic type from a typeof expression Pin
leppie5-Jul-08 11:54
leppie5-Jul-08 11:54 
GeneralRe: Initiating a generic type from a typeof expression Pin
Lutosław6-Jul-08 1:03
Lutosław6-Jul-08 1:03 
GeneralRe: Initiating a generic type from a typeof expression Pin
leppie6-Jul-08 3:49
leppie6-Jul-08 3:49 
GeneralRe: Initiating a generic type from a typeof expression Pin
Lutosław6-Jul-08 8:04
Lutosław6-Jul-08 8:04 
Situation has changed, because it turned out that different types requiere different methods, so a bunch of "is" cases was needed anyway.

But the subject is interesting, so assuming that I need the same thing which I needed before, the situation comes out as follows.

Let be an Element class and a set A of n XElement classes deriving from Element.
Let be a sequence of objects s and for each object o in s { type of o is in A }.
Let be a method M(start, end).
Let be a function f(o) which returns some data related to an object o from sequence s.
The method M iterates through a part [start,end] of s and needs some information of every element o. So it calls function f(o) for each o in s. f is defined as follows:
internal T lastElementOfType<T>(int start) where T : Element // <= this is f
{
	int index = elements.FindLastIndex(start, el => el is T); // 'elements' is s
	return index == -1 ? null : (T)elements[index];
}
internal T lastElementOfType<T>(Element start) where T : Element
{
	return lastElementOfType<T>(findIndex(start));
}
int findIndex(Element element)
{
	return elements.FindIndex(el => object.ReferenceEquals(el, element));
}

Now.

In the implementation, s is a list of Elements, but objects of this list aren't of type Element but of one of types from A (from the definition of s given above). So I made f generic method which takes typeof(o) as a generic parameter. Unfortunately, passing an expression typeof(o) as T generates a compilation error. This is reasonable because typeof is evaluated at runtime. So what to do?*

* - I've got some ideas but I don't want to be a killjoy you know.

Greetings - Gajatko

Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

GeneralRe: Initiating a generic type from a typeof expression Pin
leppie6-Jul-08 8:25
leppie6-Jul-08 8:25 
QuestionRSA key exchange Pin
nyjcr5-Jul-08 7:04
nyjcr5-Jul-08 7:04 
QuestionOverriding custom contol OnPaint Pin
hpjchobbes5-Jul-08 6:54
hpjchobbes5-Jul-08 6:54 
AnswerRe: Overriding custom contol OnPaint Pin
Lutosław5-Jul-08 10:14
Lutosław5-Jul-08 10:14 
QuestionMDI Parent and Child Pin
benjamin yap5-Jul-08 5:27
benjamin yap5-Jul-08 5:27 
AnswerRe: MDI Parent and Child Pin
hpjchobbes5-Jul-08 8:04
hpjchobbes5-Jul-08 8:04 
GeneralRe: MDI Parent and Child [modified] Pin
benjamin yap6-Jul-08 0:08
benjamin yap6-Jul-08 0:08 
GeneralRe: MDI Parent and Child Pin
DaveyM696-Jul-08 1:33
professionalDaveyM696-Jul-08 1:33 
GeneralRe: MDI Parent and Child [modified] Pin
benjamin yap6-Jul-08 1:38
benjamin yap6-Jul-08 1:38 
GeneralRe: MDI Parent and Child Pin
DaveyM696-Jul-08 1:59
professionalDaveyM696-Jul-08 1:59 
GeneralRe: MDI Parent and Child Pin
benjamin yap6-Jul-08 2:07
benjamin yap6-Jul-08 2:07 
GeneralRe: MDI Parent and Child Pin
DaveyM696-Jul-08 2:18
professionalDaveyM696-Jul-08 2:18 
GeneralRe: MDI Parent and Child [modified] Pin
DaveyM696-Jul-08 4:16
professionalDaveyM696-Jul-08 4:16 
GeneralRe: MDI Parent and Child [modified] Pin
benjamin yap6-Jul-08 4:48
benjamin yap6-Jul-08 4:48 
GeneralRe: MDI Parent and Child Pin
DaveyM696-Jul-08 5:45
professionalDaveyM696-Jul-08 5:45 
GeneralRe: MDI Parent and Child [modified] Pin
benjamin yap6-Jul-08 6:00
benjamin yap6-Jul-08 6:00 
GeneralRe: MDI Parent and Child Pin
DaveyM696-Jul-08 8:12
professionalDaveyM696-Jul-08 8:12 

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.