Solved

c# how do I pass a class in as a parameter to a function

Posted on 2010-11-27
5
485 Views
Last Modified: 2013-12-17
I have a function that uses the lines below.  I would like to pass in the class to the function instead of how it is hardcoded now.  Is this possible?

string evClass = typeof(my.classname).FullName;
string evAssembly = typeof(my.classname).Assembly.FullName;

where my.classname is actually a class  
0
Comment
Question by:jackjohnson44
  • 3
5 Comments
 
LVL 6

Expert Comment

by:AlfredRobot
ID: 34223891
0
 

Author Comment

by:jackjohnson44
ID: 34223893
Thanks, but I don't understand that at all.  Can you please explain?
0
 

Author Comment

by:jackjohnson44
ID: 34223900
I am not trying to load an assembly either.  I plan to set everything to a variable, and don't mind presetting it and not using it.

I want
var x = class1
var y = class2

If I want to use x or y, I just use them.  I don't need to load anything at runtime.  I just don't want to have to have two different versions of the same function.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34224924
You can either pass a Type to your method directly, use an overload (depending on how many types you want to use), or use a Generic method:
        private void DoSomething<T>()
        {
            string className = typeof(T).FullName;
            string classAsm = typeof(T).Assembly.FullName;
        }

// call method
DoSomething<Car>();
DoSomething<Person>();

Open in new window

0
 

Author Closing Comment

by:jackjohnson44
ID: 34232075
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question