How to search function using the description of this function? - c#

I want to find necessary function by writing some key words or phrase from description of needed function. This need in cases, when I dont remember the name of function, but I know what it must do.
For examle: I want to produce sequence from two sequences. But I dont remember the function name.
Needed function called "Zip". This function has description with my wishes.
Only what I can is to search my wishes using the sources, like Google, StackOverflow and so on. I think that Visual Studio has all opportunities to find function using only its description.
But I dont know how to do this. It would be cool to find all the functions contain needed request in description and I could choose the fitting in my case.

I recommend that you do so using reflection. Add descriptions to each method like this.
Then, you can use the GetCustomAttributes class method to get a list of descriptions:
var descriptions = (DescriptionAttribute[])
nameOfClass.GetCustomAttributes(typeof(DescriptionAttribute), false);
You can iterate through your descriptions to find the correct methods to call, and you can also do this through reflection like so:
MethodInfo methodInfo = nameOfClass.GetMethod(methodName);
object result = methodInfo.Invoke(classInstance, parametersArray);
I am sure you will want to do more error checking and handling, but this at least explains how to go about doing this.
You can then search for your methods based on how the descriptions match. I'm not how you prefer to go about doing so, but I'd be happy to help.

Related

Get selected items in solution with native interfaces

I am trying to get all selected items in a solution and this with native code. With native code I am referring to code which does not use the DTE.
I checked the documentation and tried to find a suitable solution, however I din't come very far. What I found was the IVsUiHierarchy which contains the ExecCommand method which contains the following.
Commands that act on a specific item within the hierarchy. (If ItemID equals VSITEMID_SELECTION, the command is applied to the selected item or items.)
So I suspect the method they are talking about, is the before mentioned ExecCommand one. For one I am not quite sure how I will get to the IVsHierarchy object from a IVsHierarchy or similar, on the other hand I am not really sure on how to use the ExecCommand method properly. Additionally I am not even quite certain, if this is even the 'right way' of approaching this.
Note: I am searching for a solution which does not contain the following code in this answer.
You can use IVsMonitorSelection.GetCurrentSelection, to identify all the selected items in the solution explorer.
The above will return you an IVsMultItemSelect interface which you can use to invoke IVsMultiItemSelect.GetSelectedItems to retrieve an array of VSITEMSELECTION values.
There are a few extensibilty samples, that utilize GetSelectedItems, you can use as a reference.
Sincerely,
Ed Dore

Scripting functoid returns input instead of return-value in Biztalk mapper

I have a scripting functoid with the following code:
public string MyConcat(string product)
{
string retStr= "01";
product = product.ToUpper();
if(product.Contains("CONDITION")){
retStr= "02";
}
return retStr;
}
This works perfect when I run it in LinqPad, but when I test the map it returns the product string instead of the retStr, which I find really weird. Any help is much appreciated.
You probably have another Scripting functoid that has the same signature, i.e. is called MyConcat, returns a string, has a single string input. In that case it will execute the first version created with the input linked to it.
Please make sure you give your function names a unique and descriptive name to avoid this.
If you do need to use the same function multiple times in your map, this feature of it re-using the the function is quite useful, but I usually make sure to add a comment to all the subsequent copies stating that only the first version has the code.

What is this code doing?

Could someone please explain to me what the following lines of code do?
dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
string path = System.IO.Path.GetDirectoryName(filePath);
string fileName = System.IO.Path.GetFileName(filePath);
dynamic directory = shellApplication.NameSpace(path);
dynamic link = directory.ParseName(fileName);
dynamic verbs = link.Verbs();
I've searched the msdn library, but couldn't really understand what it did.
This isn't the full code, but I undertand the rest, it is just this part that I'm struggling with.
Looks like it is retrieving the shell actions that a particular program is associated with. For example Open, Print, Edit, etc.
Open regedit and navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\textfile
Expand it out and look at the Shell key. The code should be returning verbs similar to that.
This creates "Shell.Application" COM object and then uses dynamic to call methods on it.
It gets all the verbs that can be called on a file.
This is basically scripting. See here and here for a sample.
To expand on Aliostad's answer, the dynamic keyword in C# allows you to call members and methods on an unknown type. This means using a dynamic variable you won't get intellisense since the compiler has no clue what members or methods the variable actually has. This is all figured out at runtime.
Here is a good explanation.

how to achieve calling a function dynamically, thats right the function to be called is decided from database values, using c#

the application is very large so giving a brief back ground and the problem
when the user logs in, a button is displayed having the text of the function he is allowed to call.
the function he is allowed is mapped in the database table
its made sure that the name of the actual function is same to the ones used in the db.
problem
the name is extracted, and stored as text field of button and also in a string variable.
now how am i supposed to call this function using the string variable which has the name stored in it!
like we type
name-of-function();
but here i dont know the name, the string at run time does so i cant write like
string()!!?
You will need to use reflection to do this. Here is a rough sketch of what you need to do:
// Get the Type on which your method resides:
Type t = typeof(SomeType);
// Get the method
MethodInfo m = t.GetMethod("methodNameFromDb");
// Invoke dynamically
m.Invoke(instance, null);
Depending on your actual needs you will have to modify this a little - lookup the used methods and types on MSDN: MethodInfo, Invoke
Well, no matter what you do, there is going to have to be some kind of mapping done between a database "function" and your "real" function. You can probably use Reflection using your Types and MethodInfo.
However, this sounds like a maintenance nightmare. It also sounds like you are reinventing user roles or the like. I would be very cautious about going down this path - I think it will be much more complex and problematic than you think.

Dynamic "WHERE" like queries on memory objects

What would be the best approach to allow users to define a WHERE-like constraints on objects which are defined like this:
Collection<object[]> data
Collection<string> columnNames
where object[] is a single row.
I was thinking about dynamically creating a strong-typed wrapper and just using Dynamic LINQ but maybe there is a simpler solution?
DataSet's are not really an option since the collections are rather huge (40,000+ records) and I don't want to create DataTable and populate it every time I run a query.
What kind of queries do you need to run? If it's just equality, that's relatively easy:
public static IEnumerable<object[]> WhereEqual(
this IEnumerable<object[]> source,
Collection<string> columnNames,
string column,
object value)
{
int columnIndex = columnNames.IndexOf(column);
if (columnIndex == -1)
{
throw new ArgumentException();
}
return source.Where(row => Object.Equals(row[columnIndex], value);
}
If you need something more complicated, please give us an example of what you'd like to be able to write.
If I get your point : you'd like to support users writting the where clause externally - I mean users are real users and not developers so you seek solution for the uicontrol, code where condition bridge. I just though this because you mentioned dlinq.
So if I'm correct what you want to do is really :
give the user the ability to use column names
give the ability to describe a bool function (which will serve as where criteria)
compose the query dynamically and run
For this task let me propose : Rules from the System.Workflow.Activities.Rules namespace. For rules there're several designers available not to mention the ones shipped with Visual Studio (for the web that's another question, but there're several ones for that too).I'd start with Rules without workflow then examine examples from msdn. It's a very flexible and customizable engine.
One other thing: LINQ has connection to this problem as a function returning IQueryable can defer query execution, you can previously define a query and in another part of the code one can extend the returned queryable based on the user's condition (which then can be sticked with extension methods).
When just using object, LINQ isn't really going to help you very much... is it worth the pain? And Dynamic LINQ is certainly overkill. What is the expected way of using this? I can think of a few ways of adding basic Where operations.... but I'm not sure how helpful it would be.
How about embedding something like IronPython in your project? We use that to allow users to define their own expressions (filters and otherwise) inside a sandbox.
I'm thinking about something like this:
((col1 = "abc") or (col2 = "xyz")) and (col3 = "123")
Ultimately it would be nice to have support for LIKE operator with % wildcard.
Thank you all guys - I've finally found it. It's called NQuery and it's available from CodePlex. In its documentation there is even an example which contains a binding to my very structure - list of column names + list of object[]. Plus fully functional SQL query engine.
Just perfect.

Categories

Resources