Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 24
Hey Audio! 3 29
REXEX help Part 2 2 23
Help with consolidating excel files using VB.net 2 23
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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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