开发者

Dynamic Binding Flag Algorithm which uses RegExp

I've be开发者_如何学运维en at this for at least 3 hours now - 1.5 - 2 of which has been spent just learning regex in order to do this. I am still nowhere near comprehending it, but that's not quite the priority: I'd rather get this algorithm flushed out first.

SO..

Here, I have a nice little method which accepts a string which is basically supposed to contain a phrase that determines what kind of bindingflag to return. The string is evaluated by the regexpression. If this expression comes out to true, it goes onward to move through an array which lists the proper BindingFlags settings to set. I have most of this figured out - the main problem is A) simplicity, and B) the last two bits: ensuring that a BindingFlag will be returned (Which I believe I have done, but I still get a logic error from that, so obviously I haven't), and comparing the string with the actual BindingFlag array to determine which flag to select and return.

Kind of complicated for my level. Here's the code:

private static BindingFlags CheckPropBinding(string bindFlagSpec) 
        {
            BindingFlags binderFlag; //--will be returned.

            string bindLower = bindFlagSpec.ToLower(); //--lowers string parameter.

            Match matchBinder; //--our matcher :).

            string regex = "regex dummy"; /*--this is a dummy which will be replaced when I've found the appropriate regex to use. */

            matchBinder = Regex.Match(bindLower, regex); 

            if (matchBinder.Success) //--if success, will go on...
            {
                for(int bindIndex = 0; bindIndex < bindings.Length; bindIndex++) /*--compares string with bindingFlag array */
                {
                    if (bindLower.Contains(bindings[bindIndex].ToString()))
                    {
                        binderFlag = bindings[bindIndex];

                        break;
                    }
                    else 
                    {
                        if (bindIndex == bindings.Length && !bindLower.Contains(bindings[bindings.Length].ToString())) 
                        {
                             binderFlag = BindingFlags.NonPublic; /*--this is just a

last resort check, assigning a default flag if for some reason one can't be selected.*/ break; }

                        continue;
                    }

                 }
            }


            return binderFlag; //--returned :).
       }

I should probably also mention the binding flags array field:`

public class PropertyManagener
    {
        private static BindingFlags[] bindings = {BindingFlags.NonPublic, BindingFlags.Public, 
                                                     BindingFlags.Static, BindingFlags.Instance};


The error is because you're not guaranteed to be initializing binderFlag before returning it. Why not do something like this instead? This sets the binderFlag to your default return value up front, so that if your algorithm can't determine what to use, it returns NonPublic.

This at least will resolve the compiler error! I can't speak to the actual correctness of the algorithm you've implemented, unfortunately.

    private static BindingFlags CheckPropBinding(string bindFlagSpec)         
    {
        BindingFlags binderFlag = BindingFlags.NonPublic;
        string bindLower = bindFlagSpec.ToLower(); //--lowers string parameter.            
        Match matchBinder; //--our matcher :).            
        string regex = "regex dummy"; /*--this is a dummy which will be replaced when I've found the appropriate regex to use. */            
        matchBinder = Regex.Match(bindLower, regex);
        if (matchBinder.Success) //--if success, will go on...            
        {
            foreach (BindingFlags t in bindings)
            {
                if (bindLower.Contains(t.ToString()))
                {
                    binderFlag = t;
                    break;
                }
            }
        }
        return binderFlag; //--returned :).       
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜