|
George_George wrote: Means using BindingFlags.NonPublic to grant permission?
No, permission is different and is part of Code Access Security (CAS), which is an entirely different topic. To grant permission you would need to add a ReflectionPermission attribute to the code somewhere (not sure where since I don't know the code you are using and don't do a lot with CAS and reflection.
George_George wrote: I am confused what means "outside the current assembly" and why it applies to my situaion, since I am always using the same assembly -- the current.
"Outside the current assembly" means just that - the calling code is located in a different physical assembly than the code being called. This may not be the case.
What it comes down to is that you really should think of the binding flags like a combination of this:
1. BindingFlags.Instance or BindingFlags.Static
2. BindingFlags.Public or BindingFlags.NonPublic or ... (any of the other flags)
By just specifying BindingFlags.Instance you are effectively saying get me any instance field that is neither public nor non-public, which really doesn't leave a lot.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
Thanks Scott!
1.
"By just specifying BindingFlags.Instance you are effectively saying get me any instance field that is neither public nor non-public" -- where do you find this point? From MSDN I can not find out. Could you post a link about where you find the statemen please? I want to learn more.
2.
I think CAS does not apply to my sample, and also "Outside the current assembly" also does not applies to my sample. Correct?
(please refer to my code in my original very first question post)
3.
What members are neither public nor non-public? Interested to know.
regards,
George
|
|
|
|
|
George_George wrote: By just specifying BindingFlags.Instance you are effectively saying get me any instance field that is neither public nor non-public" -- where do you find this point? From MSDN I can not find out. Could you post a link about where you find the statemen please? I want to learn more.
There really isn't a direct link for this. It comes from reading the MSDN documentation (more correctly, reading "between the lines") and experience.
George_George wrote: I think CAS does not apply to my sample, and also "Outside the current assembly" also does not applies to my sample. Correct?
Correct. From your other post, CAS doesn't really apply; nor does "outside the current assembly".
George_George wrote: What members are neither public nor non-public? Interested to know.
There really aren't any. (That's why I had the ). Access modifiers can be broken down like this:
Public, meaning is "access not limited".
Protected, meaning "access limited to the containing class or types derived from the containing class".
Internal, meaning "access limited to this program".
Protected internal (which is protected or internal), meaning "access limited to this program or types derived from the containing class".
Private, meaning "access limited to the containing type".
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
Great Scott!
1.
Scott Dorman wrote: There really isn't a direct link for this. It comes from reading the MSDN documentation (more correctly, reading "between the lines") and experience.
That's fine. Could you post the link please so that I can also have a learn?
2.
Could I understand "internal" is used to restrict access only to the current assembly (e.g. other assembly can not reference this "internal" type)?
regards,
George
|
|
|
|
|
George_George wrote: Could you post the link please so that I can also have a learn?
Sure, the link is: http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx[^]
George_George wrote: Could I understand "internal" is used to restrict access only to the current assembly (e.g. other assembly can not reference this "internal" type)?
Yes, internal restricts access to only the current assembly.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines][ Articles][ Blog]
|
|
|
|
|
Great Scott!
What means "When overridden in a derived class, searches for the fields defined for the current Type" in the document? I am especially confused about what means "overridden in a derived class" and "defined for the current Type". Could you show me some pseudo code please?
regards,
George
|
|
|
|
|
You told it you wanted fields that are neither Public nor NonPublic; you should specify at least one.
Same with Instance and Static I presume.
|
|
|
|
|
Thanks PIEBALDconsult!
1.
Sorry my English is not very good.
You mean the behavior of using Instance tag alone is -- "wanted fields that are neither Public nor NonPublic"?
2.
If the answer to 1 is yes, then I think it is useless to use the Instance tag alone, right?
regards,
George
|
|
|
|
|
|
|
Those links don't work right for me.
|
|
|
|
|
Hi PIEBALDconsult,
The link is,
http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx[^]
Question is,
--------------------
What means "When overridden in a derived class, searches for the fields defined for the current Type" in the document? I am especially confused about what means "overridden in a derived class" and "defined for the current Type". Could you show me some pseudo code please?
--------------------
regards,
George
|
|
|
|
|
George_George wrote: what means "overridden in a derived class"
Ignore that.
George_George wrote: "defined for the current Type".
Fields in the type.
|
|
|
|
|
Thanks PIEBALDconsult,
It is great!
regards,
George
|
|
|
|
|
Hello everyone,
Here is my code segment for a thread, which will sleep one second, do some work and check if the stop status is set -- if set, then break the loop and stops the method.
My question is, whether it is possible to use event (e.g. ManualResetEvent) to implement similar logics?
void ThreadMethod1()
{
while (true)
{
Thread.Sleep(1000);
if (stop)
{
}
else
{
}
}
}
thanks in advance,
George
|
|
|
|
|
You could use a semaphore and wait for it to unblock.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian,
I think in this way, my thread will be forever blocked until the signal is set, right? But my purpose is to do some work at 1 second interval and checks for stop at the same interval -- if the stop signal is not coming, other than blocking forever, I want the thread to continue to do its work.
Any comments?
regards,
George
|
|
|
|
|
Well, then you're on the right track with a timer.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
1.
Do you mean using Timer to check stop status or to do the regular work?
2.
If you mean using Timer to do the regular work, I think the Timer class is used to perform a task in a regular interval no matter whether or not the previous task is completed or not. So, it is possible to execute two tasks at the same time if the 1st task is not completed in 1 second for some reason. Right?
How to avoid such situation?
regards,
George
|
|
|
|
|
1 - if you want to execute code regularly, use a timer
2 - Your timer is not going to fire again if that thread is busy. So, it's not going to run your code twice at the same time, it's single threaded. If your code takes 30 seconds to run and you have a 20 second timer, the timer will just fire late.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
You mean using timer and in the timer execution method, we do two work items -- checks both the stop status and do the regular work?
regards,
George
|
|
|
|
|
Yeah, I guess so.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks Christian,
The timer solution works. A further question, what are the pros and cons compared with using sleep and using timer from functional and performance perspective?
regards,
George
|
|
|
|
|
Christian Graus wrote: it's not going to run your code twice at the same time
I find that not to be the case (with a System.Timers.Timer anyway, other Timers may differ).
private System.Timers.Timer tt = new System.Timers.Timer ( 1000 ) ;
public Template
(
)
{
tt.Elapsed += F ;
tt.Start() ;
return ;
}
private void
F
(
object sender
,
System.Timers.ElapsedEventArgs e
)
{
System.Console.WriteLine ( "Start" ) ;
System.Threading.Thread.Sleep ( 5000 ) ;
System.Console.WriteLine ( "Stop" ) ;
return ;
}
Sample output:
Start
Start
Start
Start
Start
Stop
Start
Stop
Start
Start
Stop
Start
Stop
Stop
Start
Stop
Start
Stop
Start
Stop
Start
Stop
Start
|
|
|
|
|
George_George wrote: I think the Timer class is used to perform a task in a regular interval no matter whether or not the previous task is completed or not. So, it is possible to execute two tasks at the same time if the 1st task is not completed in 1 second for some reason. Right?
AFAIK, timer's execution will be blocked until it finishes the job. So you won't be able to run two tasks in parallel in normal case. I think you can use a Producer/Consumer Queue[^] like things to do this. Each time timer ticks, enqueue job. So it will be executed on another thread and timer thread won't be blocked. I might be wrong here
|
|
|
|