|
As a fairly new C# programmer, I am in need of some help with reflection, or another way of something similar in C#:
I have created a control with several overloaded DisplayObject methods. I have also created an interface within the control's class similar to the following:
interface TheInterface
{
string a
{
get;
set;
}
string b
{
get;
set;
}
The DisplayObject methods have an implementation of this interface like:
DisplayObject(TheInterface myInt)
and
DisplayObject(TheInterface[] myInts)
I have these two methods working just fine. But, I want to add to them......
Outside of the control I define two classes like:
class Outside1 : TheInterface
class Outside2 : TheInterface
And I define another class such as
class WayOutside
{
class Outside1 myOutside1;
class Outside2 myOutside2;
}
Then, the user of my control would call:
DisplayObject(WayOutside);
To handle that case, what I would like to do inside of my control is have another DisplayObject method that would take a generic variable (maybe an object type?) and then have my control go through the class and call the other DisplayObject method that takes either of the "TheInterface" methods above so that the code might look like:
void DisplayObject(object generic)
{
.... HELP HERE? Reflection? Another way? .....
foreach(TheInterface item in generic)
this.DisplayObject(item);
}
Basically, the WayOutside class can have any number of classes in it, but they will all implement TheInterface.
Anyone have any ideas as to how I can do something like this?
Thanks!
David
-- modified at 13:06 Thursday 17th August, 2006
|
|
|
|
|
Yes, reflection sounds like a good way to go. If you're dealing with properties, then you'd do something like:
foreach (PropertyInfo pi in generic.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
this.DisplayObject(pi.GetValue(generic, null) as TheInterface);
}
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
When I read "fairly new C# programmer" and "reflection" in the same sentence, I want to say this:
In my opinion, reflection is one of the most over-used aspects of programming that I know. It seems like almost everyone runs for the Golden Hammer of Reflection as soon as they have something that they don't know how to implement.
If I had an object that contained other objects that I wanted, I would make a method in the object that handed me the objects. You kan make an interface to force the class to implement the method:
interface IGotMilk {
TheInterface[] GetTheInterfaceObjects();
}
void DisplayObject(IGotMilk generic) {
foreach (TheInterface item in generic.GetTheInterfaceObjects()) {
this.DisplayObject(item);
}
}
---
b { font-weight: normal; }
|
|
|
|
|
Thank you both for your help and suggestions. I originally considered implementing this with the array of TheInterface objects within the implementer, but nixed the idea, because I want the implementer to have to do as little work as possible to use my class.
Having said that, I did get the array implementation to work correctly as well as the reflection example to work correctly also.
Thank you again for your help....
David
|
|
|
|
|
I'm feeling lazy: Has anyone cobbled together a replacement for DirectoryInfo.GetFiles that accepts multiple file filters?
-- modified at 13:42 Thursday 17th August, 2006
OK, here's something quick and dirty...
private void AddFilesRecursively(List<FileInfo> files, DirectoryInfo directory,
string filters, bool recurse)
{
FileInfo[] dirFiles = directory.GetFiles("*.*", SearchOption.TopDirectoryOnly);
if (filters == "*.*")
files.AddRange(dirFiles);
else
{
foreach (FileInfo file in dirFiles)
{
if (Regex.IsMatch(file.Extension, filters, RegexOptions.IgnoreCase))
files.Add(file);
}
}
if (recurse)
{
DirectoryInfo[] dirInfos = directory.GetDirectories();
foreach (DirectoryInfo dir in dirInfos)
{
AddFilesRecursively(files, dir, filters, recurse, level+1);
}
}
}
private List<FileInfo> GetFiles(string path, string filters, bool recurse)
{
List<FileInfo> files = new List<FileInfo>();
DirectoryInfo dirInfo = new DirectoryInfo(path);
AddFilesRecursively(files, dirInfo, filters, recurse, 0);
return files;
}
-- modified at 13:48 Thursday 17th August, 2006
|
|
|
|
|
Unfortunately not. There was a post about it a little while ago and nobody seemed to offer up anything. Maybe I should write one that takes a Regex...
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
Hi Chris
Would something like this work?
public IEnumerable<string> GetFilesInDirectory(string directory, SearchOption searchOptions, params string[] filters)
{
foreach (string filter in filters)
{
foreach (string matchingFile in Directory.GetFiles(directory, filter, searchOptions))
{
yield return matchingFile;
}
}
}
Use it like this:
IEnumerable<string> txtAndXmlFiles = GetFilesInDirectory(@"c:\foo\", SearchOption.AllDirectories, "*.txt", "*.xml");
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: And in this corner, the Party of Allah
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Something like that was my first thought. Unfortunately it means the directory tree is being recursed for each filter, and if you have, say, 50,000 files then it bogs down.
Why, Microsoft? Why...
|
|
|
|
|
Ok - what if we did a GetFiles call, returning all files in the directory, then doing the filter ourselves? Would that be too much overhead too? If so, you're faced with implementing a custom GetFiles, in which case, MSDN has already done this for you[^].
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: And in this corner, the Party of Allah
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Thanks for the link. That's a great way to use yield return.
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
While this method is way, way faster, it still doesn't address the issue of multiple filters. However, it does give me a much nicer base on which to hack
Thanks.
|
|
|
|
|
|
Better watch out, Professor Sharada Ulhas has been stealing your sig...
Logifusion[^]
If not entertaining, write your Congressman.
|
|
|
|
|
|
Hi,
sorry for posting again, but problem hasn't been solved jet. I know this might be difficult, but perhaps somebody knows:
my starting routine looks like this:
private void StartProcess()
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Programme\\AutoGK\\AutoGK.exe";
p.Start();
}
When I run the code everything is executed fine except that the application to be started shows me an error: "Installation is corrupted. Please reinstall the application." BTW: The code works with any ohter application I have tested so far. But starting the app (AutoGK) manually (in windows explorer via double click, etc.) causes no errors and the application starts fine. The program I want to start is AutoGordianKnot, which probably was written in Qt. Any suggestions?
Greetings
|
|
|
|
|
|
If I run AutoGK.exe from command line it starts fine. No problems.
|
|
|
|
|
Is your application being run from a different user account than your are developing? That could cause an issue, esp with ASP.NET.
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
No, it is not. BTW: I'm developing a usual c# windows application.
|
|
|
|
|
|
That is already the default. Setting it to false also doesn't solve.
|
|
|
|
|
There is a chance that the AutoGK application is looking for some support file and cannot find it when started from a .NET app.
When started from explorer or from the command-line, it is able to find support files automatically. Explorer knows where to look for support files as an app is always configured with a startup directory.
Starting from a .NET app it may not know where it's home directory is (i.e., there maybe some Field that needs to be set to tell it where it's home directory is).
Just a thought.
|
|
|
|
|
Thank you all guys.
@Mike Bluett: You're right, that really helped me out. Setting the working directory for the process tells it where to look for the additional files. Thanks.
|
|
|
|
|
Does anyone know a way to list the components of a COM+ Application using C#? Specifically, I would like to be able to determine how many components in a specific COM+ package are active, how many are pooled, how many are in call, their call time, their ProgID and their CLSID.
I need to basically be able to programatically get at the information that is available in Component Services.
Thanks.
Jack
|
|
|
|
|
I have a program that I have written that saves encrypted files and text to an Access database. Everything is converted to a byte array and saved as an OLE object.
Number 1: Is there a maximum file size that can be saved to access using this method?
If not, is there a known issue with using Update statements to this kind of field? i can insert just fine, and even update works to a point, but after a while, i try updating a file and it doesnt work any more. Let me know if you need more details or code snippets.
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|