Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,

I am using following code for calling single arguments of constructor

C#
List<MsgIdTimeStampMap> CANMsgIdList = new List<MsgIdTimeStampMap>();
          
           CANMsgIdList=  ReadLogFile(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\log file with orange\logfile.asc");


           var ctors = typeof(MsgIdTimeStampMap).GetConstructors();

           var ctor = ctors[0];

           ParameterInfo[] pars = ctor.GetParameters();
           Console.WriteLine(pars[2]);
           Console.ReadKey();


Result on console:

System.String timestamp

This is working fine. But when I am trying to do something like this

C#
var ctors = typeof(CANMsgIdList[0]).GetConstructors();


It is giving some error like this

The type or namespace name 'CANMsgIdList' could not be found (are you missing a using directive or an assembly reference?

Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Can anyone help me in solving this???


Thanks
John
Posted
Comments
BillWoodruff 20-Dec-13 10:24am    
This, and your previous question along the same lines, suggest to me you are having some difficult with examining Types at run-time, with inspecting constructor parameters, etc.

It might be helpful if you describe what your "big-picture" goal is here: why do you need to examine Types, or ctor parameters at run-time ? What type of data are you dealing with that this becomes necessary ? Is this a late-binding scenario where Types are unknown because code is being built on-the-fly ?
Sergey Alexandrovich Kryukov 20-Dec-13 11:30am    
Exactly. So far, this is the best response. I doubt OP understands the bigger picture at all. (See also my comment below :-)
It's wonderful how many totally pointless "answers" was posted, only Ron found that a bug happens well before using reflection...
—SA
Sergey Alexandrovich Kryukov 20-Dec-13 11:23am    
What does it mean, "single arguments"?
What does it mean, "calling argument" or "calling arguments"?
Why using reflection here at all? What's the purpose?
And so on...
—SA

Try:

C#
var ctors = CANMsgIdList[0].GetType().GetConstructors();


But you haven't posted all your code, because the error you are getting says that the variable CANMsgIdList can't be found in the current namespace. So you are trying to do that somewhere else that CANMsgIdList isn't defined. Please post the code you are using that doesn't work and I can better help you.
 
Share this answer
 
Comments
Karthik_Mahalingam 20-Dec-13 9:55am    
Ron, your answer is right.
its a compile error.. john should use GetType() instead of typeof()...
BillWoodruff 20-Dec-13 10:20am    
+5 Bullseye.
Sergey Alexandrovich Kryukov 20-Dec-13 11:28am    
I 5ed, too, but your comment to the question is better. :-)
The bigger problem is the purpose of using reflection here, very likely, by lack of understanding of the bigger picture by OP...
—SA
typeof is an operator where GetType is a method. typeof take integral type as an argument where GetType provide type from object at runtime. 2 are different things and usage also different.

for details:
http://stackoverflow.com/questions/983030/type-checking-typeof-gettype-or-is[^]

In your case GetType() method is appropiate for retrive constructors.
Example:
C#
class Program
    {
        static void Main(string[] args)
        {
            var emp = new Employee(1, "A");
            var list = new List<employee>();
            list.Add(emp);
            var result = list[0].GetType().GetConstructors();
            
        }
    }

    public class  Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public  Employee(int id, string name)
        {
            Id = id;
            Name = name;
        }
    }
 
Share this answer
 
v2
Try this..

C#
var ctors = CANMsgIdList[0].GetType().GetConstructors();



It is a compile error, u should use .GetType() instead of typeof(T) for this kind of situations.

read this excellent answer:
difference-of-getting-type-by-using-gettype-and-typeof[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900