Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 53
DataGridView Events ? 3 37
Error on link 14 38
VB.NET Repostiory Pattern 7 16
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 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

11 Experts available now in Live!

Get 1:1 Help Now