|
You will have to use reflection I guess
|
|
|
|
|
You can't. Generics in .NET are evaluated during runtime creating special types, [^]
The best solution to your problem is to use inheritance and base classes.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Generics are designed to be used when you know the type at compile-time (early binding). When you don't know the type until run-time (late binding), you have to use reflection.
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Ok, so how would you use reflection to make the above example work as intended?
Thanks,
Michal
|
|
|
|
|
I reexamined your example, and her is what I suggest. Since you only have 2 well-known concrete classes, you may not need reflection for now, but generics is not the way to go either.
Make an interface:
public interface IFoo
{
void MyMethod();
}
Then have some implementation, 2 in you example:
public class FooA : IFoo
{
public void MyMethod() { Console.WriteLine("Hello from FooA"); }
}
public class FooB : IFoo
{
public void MyMethod() { Console.WriteLine("Hello from FooB"); }
}
Then, using your example code:
class Program
{
static void Main()
{
Console.WriteLine("Press 1 for FooA, press 2 for FooB:");
ConsoleKeyInfo key = Console.ReadKey(true);
IFoo myFoo = null;
if (key.KeyChar == '1')
{
myFoo = new FooA();
}
else if (key.KeyChar == '2')
{
myFoo = new FooB();
}
else
{
throw new InvalidOperationException("Wrong selection, you must select 1 or 2");
}
myFoo.MyMethod();
}
}
This example is pretty simplistic, but you get the idea. In this particular case, you didn't need generics nor reflection. In real-life application, this is rarely sufficient. Read on reflection, I am sure there are quite good articles on this site.
Good luck.
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Hello,
thanks for your reply. Unfortunately, this is not what I need.
Interface was my first thought on how to solve this, but I can't use the interface because I have to be able to copy the containing type by value, not by reference. If you declare the object as an interface, you can only copy it as a reference later on (unless you unbox it in which case you - again - need to know its type
I did not include the error handling in my example as I guess we are solving something else here.
Also, as I said, the type should not be instantiated before '////' mark. It's actually being instantiated much later on in the code, but I need to know the type first.
I've been working with reflection and generics before. My above example is a much simplified illustration of what I need to do, but the main issue is still finding out the type and passing it along as a parameter.
Thanks,
Michal
|
|
|
|
|
class Program
{
static void Main(string[] args)
{
Console.WriteLine((new Bar<FooA>()).ToString());
Console.WriteLine((new Bar<FooB>()).ToString());
}
}
public abstract class BaseFoo
{
}
public class FooA: BaseFoo
{
public FooA()
{
}
public override string ToString()
{
return "FooA";
}
}
public class FooB: BaseFoo
{
public FooB()
{
}
public override string ToString()
{
return "FooB";
}
}
public class Bar<TFoo> where TFoo : BaseFoo, new()
{
TFoo tFoo = new TFoo();
public override string ToString()
{
return tFoo.ToString();
}
}
|
|
|
|
|
Thanks for your post, but it doesn't solve the problem of instantiating the class with a generic parameter on demand during run time.
Also, the generic object can't inherit the abstract class because as I said, it has to be copiable by value, not by reference.
Michal
|
|
|
|
|
You'll be looking at TypeBuilder.MakeGenericType unfortunately I think...
|
|
|
|
|
Hello, Mark,
thanks, I've looked into TypeBuilder, but this class is used to build the objects on the fly. I don't need to BUILD the obect on the fly, I already have the objects well defined beforehand. I just need a reference for that object type.
So it looks like this cannot be accomplished in C#.
|
|
|
|
|
You could have an IDictionary<type,type> and preinitialize it with {typeof(Foo), typeof(Bar< Foo >)}, {typeof(Deh), typeof(Bar< Deh >)...}
modified on Thursday, February 07, 2008 6:13:04 AM
|
|
|
|
|
Hi,
Thank for your response.Let me explain it a bit further
Say i have a server process which listens for some changes in database.
When a change occurs i want to refresh my page in browser by notyfinig it.
I do not want to refresh my page i.e. every 5 seconds, i just want to refresh it ONLY on server change just like desktop applications do.
The problem is that refreshing evry n seconds has to much impact on my web server. The refresh action should be taken only when something really happens that makes sense
Is it possible ?
|
|
|
|
|
kibromg wrote: Thank for your response.Let me explain it a bit further
What response? This is your first post in the thread.
kibromg wrote: I do not want to refresh my page i.e. every 5 seconds, i just want to refresh it ONLY on server change just like desktop applications do.
I don't think this is possible. Web applications aren't the same as desktop applications. You're going to have to poll the server periodically to refresh the page. A good compromise would be to employ caching in your ASP.NET pages so that your pages aren't being built from scratch every time you hit the server.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
trying to marshall over some data from an unmanaged dll, however i heard that the marshaller cannot marshall an array of structs, in a struct.
c++:
<br />
#pragma pack(1)<br />
typedef struct{<br />
unsigned short propCnt;<br />
PRO_PROPERTY prop[200]; <-- this sh*t here...<br />
} PRO_CHANGEDPROPERTY;<br />
#pragma pack()<br />
<br />
#pragma pack(1)<br />
typedef struct{<br />
long apiCode;<br />
PRO_DATA data;<br />
} PRO_PROPERTY;<br />
#pragma pack()<br />
c#:
<br />
[StructLayout(LayoutKind.Sequential, Pack = 1)]<br />
public unsafe struct ProChangedProperty<br />
{<br />
public UInt16 propCnt;<br />
[MarshalAs(UnmanagedType.Struct, SizeConst = 200)]<br />
public ProProperty[] prop;<br />
}<br />
[StructLayout(LayoutKind.Sequential, Pack = 1)]<br />
public struct ProProperty<br />
{<br />
public Int32 apiCode;<br />
public ProData data;<br />
}<br />
i cant seem to figure this one out, any help would be appreciated.
|
|
|
|
|
Hi All,
I have application to make and delete appointments.
Here is some code to make the appointments:
string myProp = "test";
Outlook.Application outlookApp = new Outlook.Application();
Outlook.AppointmentItem oAppointment = (Outlook.AppointmentItem)outlookApp.CreateItem (Outlook.OlItemType.olAppointmentItem);
oAppointment.Subject = "This is the subject for my appointment";
oAppointment.Body = "This is the body text for my appointment";
oAppointment.Location = "Bongerd 331";
oAppointment.Start = Convert.ToDateTime("31/01/2008 10:00:00 AM");
oAppointment.End = Convert.ToDateTime("31/01/2008 12:00:00 PM");
oAppointment.ReminderSet = true;
oAppointment.ReminderMinutesBeforeStart = 15;
oAppointment.UserProperties.Add(myProp, Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, true, System.Reflection.Missing.Value);
oAppointment.UserProperties[myProp].Value = "mijn";
oAppointment.Save();
and to delete :
String sCriteria;
string impNr = "mijn";
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");
Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items oItems = (Outlook.Items)oCalendar.Items;
sCriteria = "[test] = '" + impNr + "'";
Outlook.Items oRestrictedItems = oItems.Restrict(sCriteria);
oRestrictedItems.IncludeRecurrences = true;
Outlook.AppointmentItem oAppointment;
Outlook.AppointmentItem oAppt;
oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetFirst();
oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetNext();
sCriteria = "[test] = '" + impNr + "'";
oAppointment = (Outlook.AppointmentItem)oItems.Find(sCriteria);
if (oAppointment is Outlook.AppointmentItem)
{
oAppointment.Delete();
}
else
MessageBox.Show("No object found");
it works fine for one appointment, bud the problem is that for making the appointments i have to use a Foreach loop (depending on some situations)..so there wil be always made more then one appointments and they get all the same userproperty value...bud now wen i click on 'delete' it deletes only one of the maded appointments...how can i make a loop to delete all appointments with same userproperty value???? maybe some code?
thanx
|
|
|
|
|
I have three panels one fills the app and the other you can drag into it, but I want to nest them to stay in one column, how do I do that?
|
|
|
|
|
Is this the same question from yesterday?
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Can some one point me in the direction of some example code that allows you to use multiple text colors for the text on a tree node.
I can find examples where the text foreground/background can be overridden for the complete string, but none that show how multiple colors can be used in the same string.
Many thanks
RK
|
|
|
|
|
Hi,
I haven't done this myself (for treeviews that is) but you will need to set the DrawMode
to TreeViewDrawMode.OwnerDrawText and provide a drawing handler. Google/MSDN will provide
the details, and I trust CodeProject holds articles using it too.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
How to make form as a model Window???
Thankxxxxxxxx In Advance!!!!!!!!!
Truth Is The Simplest !!!!
|
|
|
|
|
Do you mean modal? Use .Show() or .ShowDialog() depending on your needs.
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
ThanXXXX a lot!!!!
Truth Is The Simplest !!!!
|
|
|
|
|
Hi there,
Does anybody know how I can select the entire contents of a text box from the end to the start? (thus allowing the start to be displayed in the text box, if the text is too long to fit).
Below is (obviously) selecting from left to right:
this.txtName.SelectionStart = 0;<br />
this.txtName.SelectionLength = txtName.Text.Length;
so I need something like:
this.txtName.SelectionStart = txtName.Text.Length;<br />
this.txtName.SelectionEnd = 0;
but Text Box has no SelectionEnd Property...
Thanks!
|
|
|
|
|
I didn't test it but try something like
<br />
<br />
this.txtName.SelectAll();<br />
this.txtName.SelectionStart = 0;<br />
this.txtName.ScrollToCaret();<br />
I know this kind of problems (ever tried implementing syntaxhighlighting in a richttextbox ... well you wouldn't want to use the Selects! you will end using the RTF-code itself ... argh) - so I feel you pain
|
|
|
|
|
Thanks for this CKnig, but no joy
|
|
|
|