Apex: Getting namespace prefix

I can suggest getting the namespace from the current class name..

public String getNamespacePrefix() {
    String namespacePrefix;
    String[] classNameParts = String.valueOf(<Enter className>.class).split('\\.', 2);
    if(classNameParts.size() > 1) {
        Type classType = Type.forName(classNameParts[0], classNameParts[1]);
        if(classType == <Enter className>.class) {
            namespacePrefix = classNameParts[0];
        } else {
            namespacePrefix = '';
        }
    } else {
        //If there is only one part, the class has no namespace
        namespacePrefix = '';
    }
    return namespacePrefix; 
}

If the logic is in a class known to not be an inner class then the logic can be simpler:

public class Utils {
    // Return the namespace prefix or an empty string if there isn't one
    public static String namespacePrefix {
        get {
            if (namespacePrefix == null) {
                String[] parts = String.valueOf(Utils.class).split('\\.', 2);
                namespacePrefix = parts.size() == 2 ? parts[0] : '';
            }
            return namespacePrefix;
        }
        private set;
    }
}

Can you not try like this without doing any extra efforts? If you know any custom field or custom object of your managed package then the following solution should work.

If you have a custom object with Custom_Object__c API Name

Schema.DescribeSObjectResult dsr = Custom_Object__c.sObjectType.getDescribe();
String namespacePrefix = dsr.getName().remove(dsr.getLocalName());

If you have a custom field with Custom_Field__c API Name

Schema.DescribeFieldResult dfr = Account.sObjectType.fields.Custom_Field__c.getDescribe();
String namespacePrefix = dfr.getName().remove(dfr.getLocalName());

I think this way you don't need to do any query on Apex class. Doing a query or using an apex class may lead you in a wrong path as there could be 2 managed packages with the same class name but there won't be a problem with 2 fields with the same API name since it will pick the managed package field.