methods

How to pass an Object type to Type parameter in C#

江枫思渺然 提交于 2019-12-10 04:25:00
问题 I have a function that has Type Parameter. public static object SetValuesToObject(Type tClass, DataRow dr) { // ............. // return object } I have no idea how to pass a class as parameter for this function. Here i want to pass parmeter Class "Product". I tried this SetValuesToObject(Product,datarow); But it doesn't work. How could i do this? 回答1: The typeof keyword when you know the class at compile time. SetValuesToObject(typeof(Product),datarow); You can also use object.GetType() on

Checking method visibility in PHP

强颜欢笑 提交于 2019-12-10 04:04:27
问题 Is there any way of checking if a class method has been declared as private or public? I'm working on a controller where the url is mapped to methods in the class, and I only want to trigger the methods if they are defined as public. 回答1: You can use the reflection extension for that, consider these: ReflectionMethod::isPrivate ReflectionMethod::isProtected ReflectionMethod::isPublic ReflectionMethod::isStatic 回答2: To extend Safraz Ahmed's answer (since Reflection lacks documentation) this is

How to easyily find unused public methods/properties

北慕城南 提交于 2019-12-10 03:50:00
问题 I have a .Net(C#) solution. The solution contains bunch of projects. The projects were implemented not by me. It is not a framework, it means that I need to have amount of public methods/properties as less as possible. My task is to identify methods and properties which are not used, but exist in the projects. Well, I can find private methods which are not used using R#. But it is completely unclear how to find public methods/properties which are not used. I heard that they have NDepend tool,

Having some issues with making pacman?

谁都会走 提交于 2019-12-10 03:49:07
问题 Edit: Totally forgot to mention I'm coding in Java I'm having a real hard time making some kind of detection system or some way to make my pacman sprite/character move smoothly through my board in the game. I did not make the board it's a image. I had tried colour detection first which worked the best yet was not smooth at all and pretty choppy. I then tried to manual input coordinates of location not allowed to be entered. This also did not work out so well. I'm currently trying now to have

How to judge whether a method has defined in a class?

倖福魔咒の 提交于 2019-12-10 03:05:50
问题 class C1 unless method_defined? :hello # Certainly, it's not correct. I am asking to find something to do this work. def_method(:hello) do puts 'Hi Everyone' end end end So, how to judge whether a method has defined or not? 回答1: The code you posted works just fine for checking whether the method is defined or not. Module#method_defined? is exactly the right choice. (There's also the variants Module#public_method_defined?, Module#protected_method_defined? and Module#private_method_defined?.)

Passing a method's name as a parameter

落爺英雄遲暮 提交于 2019-12-10 03:05:43
问题 private void Method1() { //Do something Log("Something","Method1"); } private void Method2() { //Do something Log("Something","Method2"); } private void Log(string message, string method) { //Write to a log file Trace.TraceInformation(message + "happened at " + method); } I have several methods like Method1 and Method2 above, and i would like some way pass the method's name as a parameter, without manually editing the code. Is that possible? 回答1: Excellent answer from Jon Skeet. However, if

Reasoning for making a method final

我怕爱的太早我们不能终老 提交于 2019-12-10 03:05:31
问题 Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason : Makes it impossible to enforce invariants. A String should behave as a String. I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot. 回答1: I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method,

Workaround for lack of return type covariance when overriding virtual methods

拥有回忆 提交于 2019-12-10 02:58:21
问题 is there any way to 'hack' or 'coerce' covariant overrides in to C#? For example: public class Alpha { public virtual Alpha DoSomething() { return AlphaFactory.GetAlphaFromSomewhere(); } } public class Beta : Alpha { public override Beta DoSomething() { return BetaFactory.GetBetaFromSomewhere(); } } Unfortunately, C# doesn't support this (which seems a bit ridiculous, but that's neither here nor there). I thought I might have an answer with method hiding: new public Beta DoSomething() {

Concurrently invoking Java method of singleton object

妖精的绣舞 提交于 2019-12-10 02:00:25
问题 I've got a question regarding multiple-thread method invocation in Java. Let's say we have a singleton object, and its class declared as follows: public class SomeClass { public void someMethod(SomeValueObject object) { if (object.condition1) { ... } if (object.condition2) { ... } if (object.condition3) { ... } } } I'm wondering if this singleton object is being concurrently accessed and its someMethod called with distinct SomeValueObject instances, is there a chance some random thread change

Record methods and const parameters in Delphi

我是研究僧i 提交于 2019-12-10 01:26:05
问题 It looks like the Delphi compiler does not honor const record parameters when "records-with-methods" are involved. Having not tried to abuse the const convention previously, I was a little surprised to find the compiler accepted code like that: type TTest = record Field : String; procedure Update; end; procedure TTest.Update; begin Field := Field + '+1'; end; procedure DoStuff(const t : TTest); begin ShowMessage(t.Field); t.Update; ShowMessage(t.Field); end; While if you try to do a t.Field:=