Solved

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

Posted on 2010-11-27
5
478 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now